我有一个Waiver
对象,其成员资格很多。我想弄清楚豁免是否有特定季度的会员资格:
def has_current_membership?
memberships.count("begin_date >= :begin_date AND end_date <= :end_date", { :begin_date => Time.now.beginning_of_quarter, :end_date => Time.now.end_of_quarter }).count > 0
end
然而,这给了我ArgumentError: Unknown key: begin_date
。但是,成员资格有一个begin_date列,因为如果我在控制台中执行Waiver.first.memberships
,我会得到:
[#<Membership id: 20, waiver_id: 1, begin_date: "2011-10-01", end_date: "2012-01-01", created_at: "2011-11-19 07:10:10", updated_at: "2011-11-19 07:10:10">]
我做错了什么?
答案 0 :(得分:7)
您应该使用.where
作为查找条件,而不是.count
两次:
memberships.where("begin_date >= :begin_date AND end_date <= :end_date", { :begin_date => Time.now.beginning_of_quarter, :end_date => Time.now.end_of_quarter }).count > 0