2013-03-28 36 views
0

(對不起,我的英語) 如果我有3個模型,電影:演員:連接,我如何讓id關聯? 連接模型有一個movie_id:integer和一個actor_id:integer,我想在演員和電影之間建立聯繫。RoR:模特協會

+1

開始由具有看看這個:HTTP://指南。 rubyonrails.org/association_basics.html – siekfried 2013-03-28 10:27:07

回答

2

你在這裏。

在電影模式

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

在Actor模型:

class Actor < ActiveRecord::Base 
    has_many :connects 
    has_many :movies, :through => :connects 
end 
在連接模式

class Connect < ActiveRecord::Base 
    belongs_to :movie 
    belongs_to :actor 
end 
+0

這會丟失一個錯誤 SQLite3 :: SQLException:no這樣的列:connections.movi​​e_id:SELECT「actors」。* FROM「actors」INNER JOIN「connected」ON「actors」。「id」=「connected」。「actor_id」WHERE「connected」。「movie_id」= 15 – 2013-03-28 11:34:55

+0

Most可能你必須運行遷移。在您的控制檯中試試這個。 'bundle exec rake db:migrate' – KULKING 2013-03-28 11:36:53