我有一个user.paid_subscriptions
集合,其中每个订阅都有created_at
(日期时间)和active
(布尔值)属性。
如何检查PaidSubscription
是否存在created_at
不等于到某个日期且active
是true
?
PaidSubscription
看起来像这样:
[
#<PaidSubscription id: 11457,
user_id: 12,
period: 3,
price: 4000,
expires_at: "2016-03-08 09:44:56",
expires_at: "2016-03-08 09:44:56",
created_at: "2015-12-08 09:44:56",
updated_at: "2016-03-08 23:00:09",
active: false,
giver_id: 20573,
partial: false,
remaining_days: 0>,
#<PaidSubscription id: 13948,
user_id: 12,
period: 1,
price: 1500,
expires_at: "2016-04-11 12:07:40",
created_at: "2016-03-11 13:07:40",
updated_at: "2016-04-11 22:00:11",
active: false,
giver_id: nil,
partial: false,
remaining_days: 0>,
#<PaidSubscription id: 11458....
]
答案 0 :(得分:0)
试试这个,
如果是查询,那么它应该是这样的
user.paid_subscriptions.where("created_at < :date or created_at > :date and active = :active",{date: DateTime.civil(yyyy,mm,dd), active: true})
或者
如果它是一个数组,你可以像下面一样使用它。
user.paid_subscriptions.any? {|ps| ps.active && ( ps.created_at.to_date < Date.civil(yyyy, mm, dd) || ps.created_at.to_date > Date.civil(yyyy, mm, dd))}