如何选择带付款的用户是真的和paid_subscriptions expirence_at当前日期?

时间:2016-07-11 13:37:12

标签: ruby-on-rails ruby activerecord rails-activerecord

我有模型用户

has_many: paid_subscriptions

模型付费订阅

belongs_to :user

我如何为所有用户编写有效记录查询,其中paidtruepaid_subscriptions expirence_at<当前日期?

编辑:

User.includes(:paid_subscriptions).where("YEAR(created_at) = ? AND active = ?", 2011, true).where('paid_subscriptions.expirence_at < ?', Date.today)

3 个答案:

答案 0 :(得分:2)

您可以尝试以下操作:

User.includes(:paid_subscriptions)
    .where('YEAR(users.created_at) = ? AND users.active = ? and paid_subscriptions.expirence_at < ?', 2011, true, Date.today)

答案 1 :(得分:0)

尝试这样的事情

user.rb

has_many :paid_subscriptions
scope :paid, -> { where(paid: true) }

paid_subscription.rb

belongs_to :user
scope :past, -> { where('experience_at < ?', Date.today) }

您在行动中的查询

User.paid.joins(:paid_subscriptions).merge(PaidSubscription.past)

答案 2 :(得分:-3)

User.find(:all, includes: paid_subscriptions, conditions: ["paid_subscriptions.expirence_at < ?", Time.now.todate])