Hello all, I having a lot of difficulty wrapping my head around the following. I''m building a site for musicians, who can post Events where they perform. So, Profile has_many :events, and Event habtm :profiles. However, I want artists to be able to add additional people to their show, such as people who aren''t registered on my site. An example. DJ Random is playing at this sweet club, together with his mate DJ John Doe. They''re both registered to my site, so looking up their profiles is easy. However, DJ Tiesto (or insert any other dj here) is playing there as well, and DJ Random wants to add Tiesto to his listing too. But, Tiesto isn''t registered to my site. I was thinking about storing the "additional artists" (Tiesto in this example) as a field in the join table, but I can''t get those fields in my event.artists collection. When I query event.artists, Rails runs this SQL command: SELECT * FROM profiles INNER JOIN artists_events ON profiles.id artists_events.artist_id WHERE (artists_events.event_id = 1 ), which obviously doesn''t get me the artists who don''t have a profile. Any ideas on how to achieve this? Thanks a bunch! -- 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 -~----------~----~----~----~------~----~------~--~---
Bart Zonneveld wrote:> Hello all, > > I having a lot of difficulty wrapping my head around the following. I''m > building a site for musicians, who can post Events where they perform. > So, Profile has_many :events, and Event habtm :profiles. > > However, I want artists to be able to add additional people to their > show, such as people who aren''t registered on my site. An example. DJ > Random is playing at this sweet club, together with his mate DJ John > Doe. They''re both registered to my site, so looking up their profiles is > easy. However, DJ Tiesto (or insert any other dj here) is playing there > as well, and DJ Random wants to add Tiesto to his listing too. But, > Tiesto isn''t registered to my site. > > I was thinking about storing the "additional artists" (Tiesto in this > example) as a field in the join table, but I can''t get those fields in > my event.artists collection. > When I query event.artists, Rails runs this SQL command: SELECT * FROM > profiles INNER JOIN artists_events ON profiles.id > artists_events.artist_id WHERE (artists_events.event_id = 1 ), which > obviously doesn''t get me the artists who don''t have a profile. > > Any ideas on how to achieve this? Thanks a bunch!has_many :through may help you though I''m not sure that''s necessarily the best approach. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 4/6/07, Michael Wang <rails-user-JtyympAsP2K7zZZRDBGcUA@public.gmane.org> wrote:> > > Bart Zonneveld wrote: > > Hello all, > > > > I having a lot of difficulty wrapping my head around the following. I''m > > building a site for musicians, who can post Events where they perform. > > So, Profile has_many :events, and Event habtm :profiles. > > > > However, I want artists to be able to add additional people to their > > show, such as people who aren''t registered on my site. An example. DJ > > Random is playing at this sweet club, together with his mate DJ John > > Doe. They''re both registered to my site, so looking up their profiles is > > easy. However, DJ Tiesto (or insert any other dj here) is playing there > > as well, and DJ Random wants to add Tiesto to his listing too. But, > > Tiesto isn''t registered to my site. > > > > I was thinking about storing the "additional artists" (Tiesto in this > > example) as a field in the join table, but I can''t get those fields in > > my event.artists collection. > > When I query event.artists, Rails runs this SQL command: SELECT * FROM > > profiles INNER JOIN artists_events ON profiles.id > > artists_events.artist_id WHERE (artists_events.event_id = 1 ), which > > obviously doesn''t get me the artists who don''t have a profile. > > > > Any ideas on how to achieve this? Thanks a bunch! > > has_many :through may help you though I''m not sure that''s necessarily > the best approach. > > > -- > Michael Wanghas_many :through is now the preferred way of many-to-many relationships. Basically, you''ll want to do: class DJ < AR::Base has_many :dj_listings has_many :venues, :through => :dj_listings end class Venue < AR::Base has_many :dj_listings has_many :djs, :through => :dj_listings end class DjListing < AR::Base belongs_to :dj belongs_to :venue end And DjListing will then be available to add an ''additional_artists'' field or whatever you see fit. Jason --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jason Roelofs wrote:> On 4/6/07, Michael Wang <rails-user-JtyympAsP2K7zZZRDBGcUA@public.gmane.org> wrote: >> > show, such as people who aren''t registered on my site. An example. DJ >> > profiles INNER JOIN artists_events ON profiles.id >> Michael Wang > has_many :through is now the preferred way of many-to-many > relationships.Thanks, it works! For others information: class Profile has_many :show_listings has_many :events, :through => :show_listings end class Event has_many :show_listings has_many :profiles, :through => :show_listings end class ShowListing < ActiveRecord::Base belongs_to :profile belongs_to :event end Event.find(id).show_listings # gives join models, with additional params set in the join table -- 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 -~----------~----~----~----~------~----~------~--~---