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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cano 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!-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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=>memberships
>
> But use it like this user.groups[0].events. Remember groups is a list.
>
>
> Florencio Cano 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!
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 Cano 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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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.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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 Rudy <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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.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 > > -- > Posted viahttp://www.ruby-forum.com/.- Zitierten Text ausblenden - > > - Zitierten Text anzeigen ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---