How to code a UNION sql query with ActiveRecord
Not solved Solved
over 1 year ago

Hi buddies, My model Coupon has 3 fields I want to query on: start_date (Date object), end_date (Date object) and immortal (Boolean) I would like to do something like this:

Coupon.where("start_date <= AND (immortal = true OR end_date >=")

But I only make bullshits :/ Please help me!

over 1 year ago

Regarding this tutorial, your code should work : (french) Did you have any displayed error message ?

over 1 year ago

It's OK I found that: DateTime::Infinity

over 1 year ago

Coupon.where("start_date =< ?","immortal = ? OR end_date >= ?", true, ?

over 1 year ago

[edit] what about using a method 'active?' in your model def active? immortal || ( >= start_date && <= expiration_date ) end Then Coupon.where( active? ) Be careful I changed the rules ('immortal' here prevails on any start/end date given) and I did not crash-tested this code.

over 1 year ago

Finally, this works:

Coupon.where("start_date =< ? AND (immortal = ? OR end_date >= ?)", , true,

Thanks to all of you

Submit your answer