我使用的是Rails 5和PostGres 9.5。我想在我的模型中搜索过去至少一年的条目,然后返回最近至少一年的条目。如果没有这样的条目,我不想要返回任何结果。我在我的模型中创建了这个...
def self.year_change(from_date)
CryptoIndexValue.
where('index_date > ?', from_date - 1.years).
order(:index_date).
take
end
但是当我今天和今天一起打电话时,我得到一个结果......
id | value | index_date | created_at | updated_at
----+-------+----------------------------+----------------------------+----------------------------
2 | 0 | 2017-07-30 15:51:52.118646 | 2017-07-30 15:51:52.147113 | 2017-07-30 15:51:52.147113
请注意" index_date"几天前回来的是什么。如何调整我的finder方法,以便它只返回至少一年的条目,否则不会返回任何结果?
答案 0 :(得分:0)
我觉得这是不道德的"对现在应该解决的问题留下答案,因此你不应该将其标记为正确的答案。但是如果你还没有解决它:
2016年比2017年小,因此1年以上的所有东西都小于今天所创造的一切:
Date.today - 2.years < Date.today
=> true
您的代码目前将创建项目的日期指定为大于-1.years
,因为它不会返回任何内容。
所以改变:
where('index_date > ?', from_date - 1.years).
要:
where('index_date < ?', from_date - 1.years).