Rails条件在几天前?

时间:2011-05-15 19:22:50

标签: ruby-on-rails ruby-on-rails-3

@messages = current_user.message_participantions.where(:read => false, :updated_at > 5.days.ago)

updated_at 5天前错误。我需要使用这样的格式:

find(:all, :conditions => ["created_at > ?", 5.days.ago])

3 个答案:

答案 0 :(得分:7)

你可以这样做:

@messages = current_user.message_participantions.where("read = ? AND updated_at > ?", false, 5.days.ago)

或者,如果由于某种原因你需要使用哈希:

@messages = current_user.message_participantions.where(:read => false, :updated_at => 5.days.ago..Time.now)

由于where方法的哈希参数值可以是精确值或范围:http://api.rubyonrails.org/classes/ActiveRecord/Base.html

答案 1 :(得分:6)

@messages = current_user.message_participantions.where(:read => false).where("updated_at > ?", 5.days.ago)

答案 2 :(得分:0)

我愿意:

@messages = current_user.message_participantions.where(["read = 0 AND updated_at > ?", 5.days.ago])