As of Rails 4, Rails automatically adds the ordering by id when you call the first
method. (You can call take
instead to avoid this.) So I'd guess that has_one
is calling first
internally.
Since has_one
is just auto-creating a method called subscription
, why not just create it yourself?:
def subscription
subscriptions.where('end_date >= ?', Date.today).order("created_at DESC").take
end
If you don't like this, it's not clear from your question whether you've tried this:
has_one :subscription, -> { where('end_date >= ?', Date.today).order("created_at DESC") }
There's no reason you can't chain the two items like that. But as I recall the Rails 4 first
will override your order
directive and replace it with order(:id)
, so this might not work. You can find out for sure by saying @user.subscription.to_sql
.