首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >你能在Rails 3搜索中对日期做大于比较吗?

你能在Rails 3搜索中对日期做大于比较吗?
EN

Stack Overflow用户
提问于 2010-11-19 19:40:21
回答 4查看 121.8K关注 0票数 127

我在Rails 3中进行了这样的搜索:

Note.where(:user_id => current_user.id, :notetype => p[:note_type], :date => p[:date]).order('date ASC, created_at ASC')

但我需要与:date > p[:date]等价的:date => p[:date]条件。我该怎么做呢?感谢您的阅读。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-11-19 19:43:55

Note.
  where(:user_id => current_user.id, :notetype => p[:note_type]).
  where("date > ?", p[:date]).
  order('date ASC, created_at ASC')

或者,您也可以将所有内容转换为SQL表示法

Note.
  where("user_id = ? AND notetype = ? AND date > ?", current_user.id, p[:note_type], p[:date]).
  order('date ASC, created_at ASC')
票数 229
EN

Stack Overflow用户

发布于 2012-10-16 22:25:36

如果遇到列名不明确的问题,可以这样做:

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())
票数 72
EN

Stack Overflow用户

发布于 2019-06-12 19:22:21

您可以尝试使用:

where(date: p[:date]..Float::INFINITY)

sql中的等效语句

WHERE (`date` >= p[:date])

结果是:

Note.where(user_id: current_user.id, notetype: p[:note_type], date: p[:date]..Float::INFINITY).order(:fecha, :created_at)

我也变了

order('date ASC, created_at ASC')

order(:fecha, :created_at)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4224600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档