繁体   English   中英

RoR:模型关联

[英]RoR: Model Associations

(对不起我的英文)如果我有3个模特,:电影:演员:连接,我怎么能成为id协会? 连接模型有一个movie_id:整数和一个actor_id:整数,我想在演员和电影之间建立联系。

干得好。

在电影模型中:

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

在演员模型中:

class Actor < ActiveRecord::Base
  has_many :connects
  has_many :movies, :through => :connects
end

在Connect模型中:

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

看起来你想做一个HABTM(Has And Belongs To Many)关系

检查这些链接是否相同

1) rails api

2)轨道铸造(免费但旧的) ,并支付

3) 我的HABTM示范项目之一

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM