.where("(start_at=?),(start_at>?), nil, Time.now)
When specifying multiple conditions such as , I would like to specify nil for the value, but I am stuttering because it does not work.In such cases, can symbols be used in conditional clauses?
If you know this, please help us!
If you use a string in where, I think you can write it as it is.
(I didn't have any data with start_at, so I tried it with created_at.Please read it again.)
Hoge.where("created_at=?OR created_at>?", nil, Time.now).to_sql
=>"SELECT`development`.`hoges`.* FROM`development`.`hoges`WHERE(created_at=NULLOR created_at>'2016-09-10 11:52:17.331907')"
However, direct SQL by string is weak for join and merge, so sometimes it is better to write with Arel.
(It will be complicated)
[5]pry(main)>Hoge.where(Hoge.arel_table[:created_at].eq(nil).or(Hoge.arel_table[:created_at].gt(Time.now))).to_sql
=>"SELECT`development`.`hoges`.* FROM`development`.`hoges`WHERE(`development`.`hoges`.`created_at`IS NULL OR`development`.`hoges`.`created_at`>'2016-09-10 11:48:54.711053')
Can I use symbols in conditional clauses?
I couldn't read the intent of this question, but is there something like this?
Thank you!!!As I wrote below, I was able to implement it as I wanted!
where("starts_at is null or starts_at>=?", now)
I wanted to use the symbol in the where conditional clause (in rails4) and if you specify nil,
Hoge.where (starts_at:nil)
Then I get the object back.
Hoge.where("(start_at=?),(start_at>?), nil, Time.now)
Then I didn't get the object back, so I was looking for a way to use symbols when specifying multiple conditions!
© 2024 OneMinuteCode. All rights reserved.