I would always, as a first solution, look at what the question being
is. You want to know if ‘a given user has a valid membership’,
use the User has_many memberships option, I would not try to turn the
question on it’s head to answer it.
Actually this post came as an echo to my previous one:
I used to benefit from Rails ability to go through object associations
so easily, and then it broke:
My Order model used to have a response_code of type integer to handle
the paid/unpaid status. And then I changed it to status of type string
with the string ‘paid’/‘unpaid’.
An suddenly I realized that I had to go over various other models to
make changes too. but that’s incorrect, if I change an attribute of the
Order, I shouldn’t be changing things in other models too. Something was
So when I query for a paid Order and an expiry date that is in the
future, where do I put the code?
I can’t put it in Order, because it would have to know about expires_at,
i can’t put it in membership, because it would have to know about the
internal state attribute of Order which already broke on me.
So where to put it?
One solution is to refactor the code, and add a redundant user_id to
each membership record and if the expires_at attribute is set to the
future, then it means that the membership has been paid.