我正在创建一个市场应用程序,卖家可以在其中列出要出售的商品。我想设定一个终止日期,这样30天以上的列表就不会出现在网站上。
我在网上发现了一些类似的例子,但无法让这个查询工作起来。
@listings = Listing.where('created_at <= ?', Date.created_at + 30.day)发布于 2014-08-25 15:49:28
您希望查询其created_at时间为>=当前日期(Date.current) - 30天(30.day)的项。因此,查询应该简单地是:
@listings = Listing.where('created_at >= ?', Date.current - 30.day)您还可以将Date.current替换为Time.now或DateTime.now。
UPDATE:作为user3334690在评论中提到的,建议您将其作为一个模型方法,因为它应该在模型层中:
# app/models/listing.rb
def self.not_expired
where('created_at >= ?', Date.current - 30.day)
end
# now in controllers you can do something like
@listings = Listing.not_expiredhttps://stackoverflow.com/questions/25489399
复制相似问题