Dimitri Dimitri - 4 months ago 9
Ruby Question

How select users where with paid is true and paid_subscriptions expirence_at current date?

I have model User

has_many: paid_subscriptions


model PaidSubscription

belongs_to :user


How me write active record query for select all users, where
paid
is
true
and
paid_subscriptions
expirence_at
< current day?

EDIT:

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

Answer

You can try the following:

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