Alright so I have a simple has_many :through relationship in Rails 3.1
class Event < ActiveRecord::Base
has_many :invites
[...]
end
class Account < ActiveRecord::Base
has_many :invites
has_many :events, :through => :invites
[...]
end
class Invite < ActiveRecord::Base
belongs_to :account
belongs_to :event
[...]
end
To get all of user’s events I do
@events = current_account.events
How can I get the record that joined the two tables (ie. the invite row) without running a query for each event? Or at least one column from the Invite record I need to reference?
Using :include => :invites includes ALL invites for each event. I just need the one the join was done on. I scoured Google and couldn’t find anything. I feel like this should be a pretty straightforward thing to do. What am I missing?
Just fetch them from the other side:
Be careful, in the
whereclause the symbols must be plural (table names) whereas in theincludesclause the symbols refer to associations (plural ifhas_many, singular ifbelongs_to). BTW, yourInvitesmodel should be singular (Invite) according to rails conventions.