I have 3 tables, one called events, one called users, and one called created_events which contains an event id and a user id (in order to link a user and an event together) - each one corresponds to its relevent model. I would like to run a query to return the created event for a certain user by passing their ID to the created_events table - and then return the relevent rows, plus the event data (linked by the event id). Is there a way to do this in rails - I could do it in SQL using joins but am a little unsure of the rails approach! -- 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 -~----------~----~----~----~------~----~------~--~---
Mike wrote:> I have 3 tables, one called events, one called users, and one called > created_events which contains an event id and a user id (in order to > link a user and an event together) - each one corresponds to its > relevent model. > > I would like to run a query to return the created event for a certain > user by passing their ID to the created_events table - and then return > the relevent rows, plus the event data (linked by the event id). > > Is there a way to do this in rails - I could do it in SQL using joins > but am a little unsure of the rails approach!FYI this is how I have done it in SQL: @created_events = CreatedEvent.find_by_sql("SELECT ce.*, e.* from created_events ce, events e WHERE (e.id = ce.event_id) AND ce.user_id = " + params[:id]) -- 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 -~----------~----~----~----~------~----~------~--~---
Hi, you can do the following to query for the events associated with a particular user: class User < ActiveRecord::Base has_and_belongs_to_many :events end class Event < ActiveRecord::Base has_and_belongs_to_many :users end Now, you can do the following to determine what events are associated with a given user: user.events Now, you can do the following to determine what users are associated with a given event: event.users Lastly, I would recommend reading chapter 18 of AWDwRv2 for further information: Good luck, -Conrad On 3/11/07, Mike <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I have 3 tables, one called events, one called users, and one called > created_events which contains an event id and a user id (in order to > link a user and an event together) - each one corresponds to its > relevent model. > > I would like to run a query to return the created event for a certain > user by passing their ID to the created_events table - and then return > the relevent rows, plus the event data (linked by the event id). > > Is there a way to do this in rails - I could do it in SQL using joins > but am a little unsure of the rails approach! > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Great - 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 -~----------~----~----~----~------~----~------~--~---
Hi Mike, your join table should be named: events_users -Conrad On 3/11/07, Conrad Taylor <conradwt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, you can do the following to query for the events associated with a > particular user: > > class User < ActiveRecord::Base > has_and_belongs_to_many :events > end > > class Event < ActiveRecord::Base > has_and_belongs_to_many :users > end > > Now, you can do the following to determine what events are associated > with a given user: > > user.events > > Now, you can do the following to determine what users are associated > with a given event: > > event.users > > Lastly, I would recommend reading chapter 18 of AWDwRv2 for further information: > > Good luck, > > -Conrad > > On 3/11/07, Mike <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > I have 3 tables, one called events, one called users, and one called > > created_events which contains an event id and a user id (in order to > > link a user and an event together) - each one corresponds to its > > relevent model. > > > > I would like to run a query to return the created event for a certain > > user by passing their ID to the created_events table - and then return > > the relevent rows, plus the event data (linked by the event id). > > > > Is there a way to do this in rails - I could do it in SQL using joins > > but am a little unsure of the rails approach! > > > > -- > > 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 -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> Hi Mike, your join table should be named: > > events_users > > -ConradHi - once I have done all that, should calling "event.users" return all the users associated with an event (as defined in the events_users table) as currently I get the error "undefined method `events'' for EventUser:Class" - I am very new to rails by the way so Im sure this is a simple error on my part!! Any ideas? Cheers Mike -- 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 -~----------~----~----~----~------~----~------~--~---
OK, first sorry for the double post! I have revised my model thus: I have a link table which stores a user id and an event id, plus two extra columns "subscribed" and "created" - users can create and subscribe to events independently. My model: class Event < ActiveRecord::Base belongs_to :user_events has_many :users, :through => :user_events class User < ActiveRecord::Base belongs_to :user_events has_many :events, :through => :user_events class UserEvent < ActiveRecord::Base has_many :events has_many :users with database tables "events", "users" and "user_events" but I am currently getting the error "uninitialized constant User::UserEvents" from this call "User.find(params[:id]).events". Is this poor pluralisation on my part? Any help is appreciated! Cheers Mike -- 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 -~----------~----~----~----~------~----~------~--~---
Hi there, i did thanks! William (Bill) Froelich wrote:> Mike, > > I''m just catching up on my mail lists. Did you get past your problem > here? > > --Bill-- 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 -~----------~----~----~----~------~----~------~--~---