I’m writing a simple membership application. I have two models, Member and Membership.
class Member < ActiveRecord::Base
has_many :memberships
end
class Membership < ActiveRecord::Base
belongs_to :member
end
A member holds information like name and date of birth, a membership holds the date the membership was applied for, the date it started, the date it expires etc. Once a membership expires, the member can renew it, which creates a new membership. This means that each member can have multiple memberships, although only the latest will be their current, valid membership.
I now want to retrieve, for example, all members whose membership has expired. I can’t just do something like
@members = Member.joins(:memberships).where('memberships.expires < ?', Time.now)
as this will include any current members who have had memberships in the past. What I really need to do is to be able to join to only the most recent of a member’s memberships and base the query on that. I’m new to Rails though so struggling a bit with this – help or ideas very much appreciated.
EDIT: Obviously this is the kind of thing that’s not difficult to do in plain old SQL, but I was hoping there’d be some nice Rails way to do it (other than just pasting the SQL in).
I would also rather not add another column to either of the tables just to make the queries I want to do possible. It’s messy, I shouldn’t have to do it, and will most likely cause problems down the line.
Thanks for all your suggestions. I’ve voted them up rather than accepting them as I don’t feel they quite cover what I want, but what I’m going to do has certainly benefitted from them.
I still feel that SQL should be avoided if possible (for all the reasons I’ve already mentioned in comments), but I think in this case it can’t, so I’ve decided to define a named scope for the Member model like this:
EDIT: I originally defined this as the default scope – however I have decided to heed the warning from KandadaBoggu on complex default scopes, so have made it a named scope instead. The query is also a bit more complicated than others described to cope with excluding renewed memberships (where the start date is in the future) when a currently active membership exists. Thanks again to KandadaBoggu for the bones of the query and hint on avoiding N+1 selects.
I’ve seen prettier bits of code. But my rationale is this – if you’re going to have to have a horrible bit of SQL like this, you might as well have it in only one place rather than repeated all over the place for every different query you might need (like expired members, members who haven’t paid yet etc etc).
With the above scope in place, I can just treat the extra membership columns as normal columns on
Member, and write nicely simple queries like this:To briefly justify accepting my own answer rather than one of the other, very useful answers, I want to point out that this was never a question about how to get the ‘greatest n per group’ (although this is a component of it). It was about how to best deal with this kind of query in the specific environment of Rails, with the specific problem of members with multiple memberships where only one is active at any one time, and with a number of similar queries that all need fields from this single active membership.
That’s why I think using a named scope in this way is, ultimately, the best answer to the question. Put up with the hideous SQL query, but have it in one place only.