如何使用has_and_belongs_to_many在Rails中保存额外的字段

时间:2010-11-04 09:20:40

标签: ruby-on-rails activerecord

我的rails应用程序中有以下结构:

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

现在MovieCeleb有2个额外字段CastName(字符串),CastType('Actor / Director)。当我保存电影时,我也创建了明星并将明星填入了明星关系中,并将movies_celebs自动保存到数据库中。但是我如何传递CastName和CastType来保存。

请告知

提前致谢。

2 个答案:

答案 0 :(得分:4)

如果您需要在连接模型上进行验证,回调或额外属性,则应使用has_many:through而不是has_and_belongs_to_many。

见:

http://guides.rubyonrails.org/association_basics.html#choosing-between-has_many-through-and-has_and_belongs_to_many

答案 1 :(得分:3)

请注意以下内容适用于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

model/movie.rb 
    class Movie < ActiveRecord::Base
      has_many :actors
      has_many :celebs, :through => :actors
    end

model/celeb.rb
    class Celeb < ActiveRecord::Base
      has_many :actors
      has_many :movies, :through => :actors
    end

model/actor.rb
    class Actor < ActiveRecord::Base
      belongs_to :movie
      belongs_to :celeb
    end

在应用程序文件夹

中测试与ruby rails控制台的关联
>script/console
>m = Movie.new
>m.name = "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 = Celeb.new
>c.name = "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
>m.save            #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 = Movie.new     #make a new movie
>m.name = "Star Wars"
>m.celebs.push(c)  #associated the existing celeb with it
>m.save
>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"
>this_actor.save

然后你可能想看看Ryan Bates的铁路广播http://railscasts.com#47(两个多对多),#73,#74,#75(复杂表格1-3部分)。

#75的网页上有多对多表单代码的更新版本。