Ruby-on-rails 您可以在 Rails 3 搜索中对日期进行大于比较吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4224600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you do greater than comparison on a date in a Rails 3 search?
提问by ben
I have this search in Rails 3:
我在 Rails 3 中有这个搜索:
Note.where(:user_id => current_user.id, :notetype => p[:note_type], :date => p[:date]).order('date ASC, created_at ASC')
But I need the :date => p[:date]condition to be equivilent to :date > p[:date]. How can I do this? Thanks for reading.
但我需要:date => p[:date]条件等于:date > p[:date]. 我怎样才能做到这一点?谢谢阅读。
回答by Simone Carletti
Note.
where(:user_id => current_user.id, :notetype => p[:note_type]).
where("date > ?", p[:date]).
order('date ASC, created_at ASC')
or you can also convert everything into the SQL notation
或者您也可以将所有内容转换为 SQL 表示法
Note.
where("user_id = ? AND notetype = ? AND date > ?", current_user.id, p[:note_type], p[:date]).
order('date ASC, created_at ASC')
回答by Sarah Vessels
If you hit problems where column names are ambiguous, you can do:
如果您遇到列名不明确的问题,您可以执行以下操作:
date_field = Note.arel_table[:date]
Note.where(user_id: current_user.id, notetype: p[:note_type]).
where(date_field.gt(p[:date])).
order(date_field.asc(), Note.arel_table[:created_at].asc())
回答by sesperanto
You can try to use:
您可以尝试使用:
where(date: p[:date]..Float::INFINITY)
equivalent in sql
等价于 sql
WHERE (`date` >= p[:date])
The result is:
结果是:
Note.where(user_id: current_user.id, notetype: p[:note_type], date: p[:date]..Float::INFINITY).order(:fecha, :created_at)
And I have changed too
我也变了
order('date ASC, created_at ASC')
For
为了
order(:fecha, :created_at)

