How to save extra fields in Rails using has_and_belongs_to_many

I have the following structure in my rails application:

class Movie < ActiveRecord::Base
  has_and_belongs_to_many :celebs, :join_table => "movies_celebs"
class Celeb < ActiveRecord::Base
  has_and_belongs_to_many :movies, :join_table => "movies_celebs"
class MovieCeleb < ActiveRecord::Base
  belong_to :movie
  belong_to :celeb

Now MovieCeleb has 2 extra fields CastName(string), CastType('Actor/Director). When I save Movie i create celebs also and fill celebs into celebs relation and it save movies_celebs automatically into database. But how can i pass the CastName and CastType to save also.

Please advise

Thanks in advance.


Note the following is for Rails v2

    script/generate model Movie id:primary_key name:string
    script/generate model Actor id:primary_key movie_id:integer celeb_id:integer cast_name:string cast_type:string
    script/generate model Celeb id:primary_key name:string

    class Movie < ActiveRecord::Base
      has_many :actors
      has_many :celebs, :through => :actors

    class Celeb < ActiveRecord::Base
      has_many :actors
      has_many :movies, :through => :actors

    class Actor < ActiveRecord::Base
      belongs_to :movie
      belongs_to :celeb

Test the associations with the ruby rails console in the application folder

>m =
> = "Do Androids Dream Of Electric Sheep"
>m.methods.sort    #this will list the available methods

#Look for the methods 'actors' and 'celebs' - these                 
#are the accessor methods built from the provided models

>m.actors          #lists the actors - this will be empty atm
>c =
> = "Harrison Ford"
>m.celebs.push(c)  #push Harrison Ford into the celebs for Blade Runner
>m.actors          #Will be empty atm because the movie hasnt been saved yet
>            #should now save the Movie, Actor and Celeb rows to relevant tables
>m.actors          #Will now contain the association for

#Movie(id : 1, name : "Do Androids..") - Actor(id : 1, movie_id : 1, celeb_id : 1) -
#Celeb(id : 1, name : "Harrision Ford")

>m =     #make a new movie
> = "Star Wars"
>m.celebs.push(c)  #associated the existing celeb with it
>movies = Movie.all   #should have the two movies saved now
>actors = Actor.all   #should have 2 associations
>this_actor = Actor.first
>this_actor.cast_type = "ACTOR"

Then you'll probably want to check out Ryan Bates' Railcasts #47 (Two Many-to-Many), and #73, #74, #75 (Complex forms parts 1-3).

There's an updated version of the many-to-many form code on the webpage for #75.

You should use has_many :through instead of has_and_belongs_to_many if you need validations, callbacks, or extra attributes on the join model.

See :

Need Your Help

Hover textbox remains fixed when selected

javascript jquery html css

I've created a search bar when, a user hover overs a button a textbox will appear. What i want to do is keep the text box to stay visible once the user has pressed the text box. So if the user

angularjs $compile service and ng-repeat

javascript angularjs

Basically what I'm trying to do is build a directive that would take array as an isolate scope object. Build html iterating through the array with ng-repeat and compiling using $compile service aga...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.