How to code a UNION sql query with ActiveRecord
Not solved Solved
almost 2 years 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!

almost 2 years ago

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

almost 2 years ago

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

almost 2 years ago

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

almost 2 years 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.

almost 2 years ago

Finally, this works:

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

Thanks to all of you

Submit your answer