2013-03-28 35 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