Hi!
I have an events table, a groups table and invitations table. Invitation
has event_id and group_id. So, Invitation belongs_to event and
belongs_to group.
I have a user table too. User has_many memberships and a membership
belongs_to user and belongs_to group.
So, A user is member of some groups that are invited to events. I want
to know the events that the groups of a user is invited to.
Something like @user.groups.events, but this throws and exception.
Thanks!
In User
has_many :groups, :through=>memberships
But use it like this user.groups[0].events. Remember groups is a list.
Khurram Ijaz.
http://www.renai-soft.com
Florencio C. wrote:
Hi!
I have an events table, a groups table and invitations table. Invitation
has event_id and group_id. So, Invitation belongs_to event and
belongs_to group.
I have a user table too. User has_many memberships and a membership
belongs_to user and belongs_to group.
So, A user is member of some groups that are invited to events. I want
to know the events that the groups of a user is invited to.
Something like @user.groups.events, but this throws and exception.
Thanks!
I think there isn’t a nice solution at this point,
what you want is;
has_many :events, :through => {:memberships => :groups}
or something,
but I don’t think that works currently,
as a slightly streamlined version of your current method;
def next_events
self.groups.map(&:events).flatten
end
but that may not be efficient, unless you find your user with
User.find(id, :include => {:groups => :events})
alternatively
def next_events
Events.find(:all,
:include => {:group => :user},
:conditions => [“users.id = ?”, self.id]
)
end
but that wont be cached.
personally I’d make a custom scoping
def events_scope
subquery = <<_SQL
SELECT * FROM invitations
INNER JOIN groups ON invitations.group_id = groups.id
WHERE
invitations.event_id = events.id
AND
EXISTS (
SELECT * FROM memberships
INNER JOIN users ON memberships.group_id = groups.id
WHERE membership.user_id = ?
)
_SQL
Event.with_scope(
:find => {
:conditions => [subquery, self.id]
}) do
return yield
end
end
def all_events
events_scope do
Event.find(:all)
end
end
but I’m mental in love with with_scope, I guess.
MatthewRudy
Florencio C. wrote:
Hello!
And what if I want to collect all the events from all the groups related
to a user?
I’m using this solution that does not seem very neat:def next_events
next_events = []
for group in self.groups
for event in group.events
next_events << event
end
end
end
Hello!
And what if I want to collect all the events from all the groups related
to a user?
I’m using this solution that does not seem very neat:
def next_events
next_events = []
for group in self.groups
for event in group.events
next_events << event
end
end
end
Khurram Ijaz wrote:
In User
has_many :groups, :through=>membershipsBut use it like this user.groups[0].events. Remember groups is a list.
Florencio C. wrote:
Hi!
I have an events table, a groups table and invitations table. Invitation
has event_id and group_id. So, Invitation belongs_to event and
belongs_to group.
I have a user table too. User has_many memberships and a membership
belongs_to user and belongs_to group.
So, A user is member of some groups that are invited to events. I want
to know the events that the groups of a user is invited to.
Something like @user.groups.events, but this throws and exception.
Thanks!
Think this plugin does what you want:
http://agilewebdevelopment.com/plugins/nested_has_many_through
seems it might also become part of the rails core in the future…
On 13 Jul., 14:02, Matthew R. [email protected]
A correction to my scoping,
Event.with_scope(:find => {
:conditions => [“EXISTS (#{subquery})”, self.id]
}) do
return yield
end
def events_scope
subquery = <<_SQL
SELECT * FROM invitations
INNER JOIN groups ON invitations.group_id = groups.idWHERE invitations.event_id = events.id AND EXISTS ( SELECT * FROM memberships INNER JOIN users ON memberships.group_id = groups.id WHERE membership.user_id = ? )
_SQL
Event.with_scope(
:find => {
:conditions => [subquery, self.id]
}) do
return yield
end
end