ruby-on-rails – 在Rails中有一个额外列的多对多表
发布时间:2020-12-17 03:39:14 所属栏目:百科 来源:网络整理
导读:Hi guys! Is possible to do this with only two Rails models,User and Event: Users|id |name |age ||1 |danilo |26 ||2 |joe |23 ||3 |carlos |50 ||4 |katy |45 |Events_Users|event_id |user_id |confirmed ||1 |1 |1 ||3 |3 |0 ||4 |3 |1 ||2 |3 |1 |E
Users |id |name |age | |1 |danilo |26 | |2 |joe |23 | |3 |carlos |50 | |4 |katy |45 | Events_Users |event_id |user_id |confirmed | |1 |1 |1 | |3 |3 |0 | |4 |3 |1 | |2 |3 |1 | Events |id |name |date | |1 |the end of the year |31/12/2012 | |2 |the end of the world |21/12/2012 | |3 |Party |18/12/2012 | |4 |Dinner |19/12/2012 |
解决方法
用户和事件具有多对多关系,您不能仅使用2个模型设置此关联,您必须具有连接模型或连接表.
在您的情况下,您添加了属性确认,因此您将需要一个名为Confirmation的连接模型(如其他人推荐的那样).您的定义关联将如下所示: class User has_many :events,through: :confirmations has_many :confirmations end class Event has_many :users,through: :confirmations has_many :confirmations end class Confirmation belongs_to :user belongs_to :event end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |