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?
3 Answers 3
.left_outer_join(:table_2, :table_1_id=>:id, :col_1=>id){Sequel[:table_2][:col_2] > date}
1 Comment
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.
1 Comment
AND statements with .where() see my updated answerThis 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)
)
4 Comments
query defined as in your example? Can you paste the output of your current example and the one I posted?.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
#find_by_sqlmethod of rails ?find_by_sqldoes not seem to work with sequel.