macarthy
2007-Feb-20 11:23 UTC
Help with model!! Do I need to use a model as a join table?
HI all, newbie question, I''m not sure exactly how I should model the following DB Structure, do I need to use a model as a join table? Here is my database actors - id - name - age movie - id - name - year role - id - name - requiresSwimming - requiresHorseskills movie_actor - actor_id - movie_id - role_id How should I map this in my model in rails? An actor can only have one role in each movie. class Role < ActiveRecord::Base end class Movie < ActiveRecord::Base end class Actor < ActiveRecord::Base end Thanks J --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten
2007-Feb-20 11:29 UTC
Re: Help with model!! Do I need to use a model as a join table?
Well, if each Role belongs to 1 specific movie and 1 actor, you don''t even need the movie_actor table... us a has_many :thorugh relationship, and roles as the join table.... actors - id - name - age movie - id - name - year role - id - movie_id - actor_id - name - requiresSwimming - requiresHorseskills class Role < ActiveRecord::Base belongs_to :movie belongs_to :actor end class Movie < ActiveRecord::Base has_many :roles has_many :actors, :through => :roles end class Actor < ActiveRecord::Base has_many :roles has_many :movies, :through => :roles end @actor= Movie.find(1).roles[1].actor # find the actor who plays the first role in the first movie On 20 Feb., 12:23, "macarthy" <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> HI all, newbie question, I''m not sure exactly how I should model the > following DB Structure, do I need to use a model as a join table? Here > is my database > > actors > - id > - name > - age > > movie > - id > - name > - year > > role > - id > - name > - requiresSwimming > - requiresHorseskills > > movie_actor > - actor_id > - movie_id > - role_id > > How should I map this in my model in rails? An actor can only have > one role in each movie. > > class Role < ActiveRecord::Base > end > > class Movie < ActiveRecord::Base > end > > class Actor < ActiveRecord::Base > end > > Thanks J--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
macarthy
2007-Feb-20 11:47 UTC
Re: Help with model!! Do I need to use a model as a join table?
I can''t do that because the Roles are a standard set e.g. Leading Man Leading Lady so although Tom Cruise can only be Leading Man once on Mission Impossible, Tom can also be Leading man once on Mission Impossible 2 so I do need that actor_roles table So how should I map that ? J On Feb 20, 11:29 am, "Thorsten" <duple...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Well, if each Role belongs to 1 specific movie and 1 actor, you don''t > even need the movie_actor table... > us a has_many :thorugh relationship, and roles as the join table.... > > actors > - id > - name > - age > > movie > - id > - name > - year > > role > - id > - movie_id > - actor_id > - name > - requiresSwimming > - requiresHorseskills > > class Role < ActiveRecord::Base > belongs_to :movie > belongs_to :actor > end > > class Movie < ActiveRecord::Base > has_many :roles > has_many :actors, :through => :roles > end > > class Actor < ActiveRecord::Base > has_many :roles > has_many :movies, :through => :roles > end > > @actor= Movie.find(1).roles[1].actor > # find the actor who plays the first role in the first movie > > On 20 Feb., 12:23, "macarthy" <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > HI all, newbie question, I''m not sure exactly how I should model the > > following DB Structure, do I need to use a model as a join table? Here > > is my database > > > actors > > - id > > - name > > - age > > > movie > > - id > > - name > > - year > > > role > > - id > > - name > > - requiresSwimming > > - requiresHorseskills > > > movie_actor > > - actor_id > > - movie_id > > - role_id > > > How should I map this in my model in rails? An actor can only have > > one role in each movie. > > > class Role < ActiveRecord::Base > > end > > > class Movie < ActiveRecord::Base > > end > > > class Actor < ActiveRecord::Base > > end > > > Thanks J--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten
2007-Feb-20 11:59 UTC
Re: Help with model!! Do I need to use a model as a join table?
ah ok i get it. the attributes like "requires_horseskills" made it look like indiviudal entries as opposed to a standard set. well then do this: actors - id - name - age movie - id - name - year role - id - name - requiresSwimming - requiresHorseskills Appearance - id - movie_id - actor_id - role_id class Role < ActiveRecord::Base has_many :appearances has_many :movies, :through => :appearances has_many :actors, :through => :appearances end class Movie < ActiveRecord::Base has_many :appearances has_many :actors, :through => :appearances has_many :roles, :through => :appearances end class Actor < ActiveRecord::Base has_many :appearances has_many :movies, :through => :appearances has_many :roles, :through => :appearances end class Appearance < ActiveRecord::Base belongs_to :movie belongs_to :actor belongs_to :role end ... so now the Model Appearance is the center of the 3 others, and each of those can find it''s corresponding items in the other 2 throught the appeances join table. On 20 Feb., 12:47, "macarthy" <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I can''t do that because the Roles are a standard set e.g. > > Leading Man > Leading Lady > > so although Tom Cruise can only be Leading Man once on Mission > Impossible, Tom can also be Leading man once on Mission Impossible 2 > > so I do need that actor_roles table > > So how should I map that ? > > J > > On Feb 20, 11:29 am, "Thorsten" <duple...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > Well, if each Role belongs to 1 specific movie and 1 actor, you don''t > > even need the movie_actor table... > > us a has_many :thorugh relationship, and roles as the join table.... > > > actors > > - id > > - name > > - age > > > movie > > - id > > - name > > - year > > > role > > - id > > - movie_id > > - actor_id > > - name > > - requiresSwimming > > - requiresHorseskills > > > class Role < ActiveRecord::Base > > belongs_to :movie > > belongs_to :actor > > end > > > class Movie < ActiveRecord::Base > > has_many :roles > > has_many :actors, :through => :roles > > end > > > class Actor < ActiveRecord::Base > > has_many :roles > > has_many :movies, :through => :roles > > end > > > @actor= Movie.find(1).roles[1].actor > > # find the actor who plays the first role in the first movie > > > On 20 Feb., 12:23, "macarthy" <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > HI all, newbie question, I''m not sure exactly how I should model the > > > following DB Structure, do I need to use a model as a join table? Here > > > is my database > > > > actors > > > - id > > > - name > > > - age > > > > movie > > > - id > > > - name > > > - year > > > > role > > > - id > > > - name > > > - requiresSwimming > > > - requiresHorseskills > > > > movie_actor > > > - actor_id > > > - movie_id > > > - role_id > > > > How should I map this in my model in rails? An actor can only have > > > one role in each movie. > > > > class Role < ActiveRecord::Base > > > end > > > > class Movie < ActiveRecord::Base > > > end > > > > class Actor < ActiveRecord::Base > > > end > > > > Thanks J- 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 -~----------~----~----~----~------~----~------~--~---
macarthy
2007-Feb-21 10:34 UTC
Re: Help with model!! Do I need to use a model as a join table?
Thanks I''ll try that On Feb 20, 11:59 am, "Thorsten" <duple...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> ah ok i get it. the attributes like "requires_horseskills" made it > look like indiviudal entries as opposed to a standard set. > well then do this: > > actors > - id > - name > - age > > movie > - id > - name > - year > > role > - id > - name > - requiresSwimming > - requiresHorseskills > > Appearance > - id > - movie_id > - actor_id > - role_id > > class Role < ActiveRecord::Base > has_many :appearances > has_many :movies, :through => :appearances > has_many :actors, :through => :appearances > end > > class Movie < ActiveRecord::Base > has_many :appearances > has_many :actors, :through => :appearances > has_many :roles, :through => :appearances > end > > class Actor < ActiveRecord::Base > has_many :appearances > has_many :movies, :through => :appearances > has_many :roles, :through => :appearances > > end > > class Appearance < ActiveRecord::Base > belongs_to :movie > belongs_to :actor > belongs_to :role > end > > ... so now the Model Appearance is the center of the 3 others, and > each of those can find it''s corresponding items in the other 2 > throught the appeances join table. > > On 20 Feb., 12:47, "macarthy" <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I can''t do that because the Roles are a standard set e.g. > > > Leading Man > > Leading Lady > > > so although Tom Cruise can only be Leading Man once on Mission > > Impossible, Tom can also be Leading man once on Mission Impossible 2 > > > so I do need that actor_roles table > > > So how should I map that ? > > > J > > > On Feb 20, 11:29 am, "Thorsten" <duple...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > Well, if each Role belongs to 1 specific movie and 1 actor, you don''t > > > even need the movie_actor table... > > > us a has_many :thorugh relationship, and roles as the join table.... > > > > actors > > > - id > > > - name > > > - age > > > > movie > > > - id > > > - name > > > - year > > > > role > > > - id > > > - movie_id > > > - actor_id > > > - name > > > - requiresSwimming > > > - requiresHorseskills > > > > class Role < ActiveRecord::Base > > > belongs_to :movie > > > belongs_to :actor > > > end > > > > class Movie < ActiveRecord::Base > > > has_many :roles > > > has_many :actors, :through => :roles > > > end > > > > class Actor < ActiveRecord::Base > > > has_many :roles > > > has_many :movies, :through => :roles > > > end > > > > @actor= Movie.find(1).roles[1].actor > > > # find the actor who plays the first role in the first movie > > > > On 20 Feb., 12:23, "macarthy" <justin.maccar...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > HI all, newbie question, I''m not sure exactly how I should model the > > > > following DB Structure, do I need to use a model as a join table? Here > > > > is my database > > > > > actors > > > > - id > > > > - name > > > > - age > > > > > movie > > > > - id > > > > - name > > > > - year > > > > > role > > > > - id > > > > - name > > > > - requiresSwimming > > > > - requiresHorseskills > > > > > movie_actor > > > > - actor_id > > > > - movie_id > > > > - role_id > > > > > How should I map this in my model in rails? An actor can only have > > > > one role in each movie. > > > > > class Role < ActiveRecord::Base > > > > end > > > > > class Movie < ActiveRecord::Base > > > > end > > > > > class Actor < ActiveRecord::Base > > > > end > > > > > Thanks J- 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 -~----------~----~----~----~------~----~------~--~---