0

I have a more complex query (does not work)

query.select_all(:table_1).select_more(:col_1)
.left_outer_join(:table_2, table_1_id: :id, col_1: 1, 
Seqel.lit('col_2 > ?', 1.week.ago)

I need to compare col_2 like this:

"col_2 > '2018-01-01 10:10:10'"

it is easy to write whole query as custom sql, but I would like to chain methods and so far I couldn't succeed in adding the above.

expected result ~:

"SELECT `table_1`.*, `table_2`.`col_1` FROM `table_1`
LEFT OUTER JOIN `table_2` 
ON (
`table_2`.`table_1_id` = `table_1`.`id`
AND
`table_2`.`col_1` = 1 
AND 
`table_2`.`col_2` > '2018-01-01 10:10:10')"

I used with_sql but it ignores other scopes. Also I tried:

 .left_outer_join("`table_2` ON (`table_2`.`table_1_id` =
 `table_1`.`id` AND `table_2`.`col_1` = 1 AND `table_2`.`col_2` >
 '2017-09-28 06:49:53 UTC')")

Problem in last one is that it wraps that whole string with extra ``


what worked for me:

.left_outer_join(:table_2, 
Sequel.lit('table_2.table_1_id = table_1.id AND table_2.col_1 = ? 
AND table_2.col_2 > ?', id, date))

but is there a way to accomplish it without using string queries?

asked Mar 27, 2018 at 7:23
2
  • Why not execute that sql in #find_by_sql method of rails ? Commented Mar 27, 2018 at 7:32
  • I could use it but I need to chain more scopes. find_by_sql does not seem to work with sequel. Commented Mar 27, 2018 at 7:36

3 Answers 3

1
.left_outer_join(:table_2, :table_1_id=>:id, :col_1=>id){Sequel[:table_2][:col_2] > date}
answered Mar 28, 2018 at 14:43
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
0

I will post my own answer, but will not accept it because I think it could be done better.

.left_outer_join(:table_2, 
Sequel.lit('table_2.table_1_id = table_1.id AND table_2.col_1 = ? 
AND table_2.col_2 > ?', id, date))

So waiting for other suggestions.

answered Mar 27, 2018 at 8:52

1 Comment

you should be able to chain AND statements with .where() see my updated answer
0

This is might be a bit cleaner:

.left_outer_join(:table_2,
 table_2.where(table_1_id: 'table_1.id')
 .where(col_1: id)
 .where('col 2 > ?', date)
 )
answered Mar 27, 2018 at 7:48

4 Comments

it does not change much
what is query defined as in your example? Can you paste the output of your current example and the one I posted?
actually I put this as second argument and put inside rest of string query and it worked: .left_outer_join(:table_2, Sequel.lit('table_2.table_1_id = table_1.id AND table_2.col_1 = ? AND table_2.col_2 > ?', id, date)), it 's cool it worked but would be nice to have more sequel-like solution
I updated my answer, but glad you figured it out., you should post your solution too.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.