ruby-on-rails – 在Rails中,使用“has_many with belongs_to”v
发布时间:2020-12-17 03:34:09 所属栏目:百科 来源:网络整理
导读:例如,在 class Student ActiveRecord::Base has_many :awardsendclass Awards ActiveRecord::Base belongs_to :studentend 以上应该是正确的用法,但是如果我们使用的话 class Student ActiveRecord::Base has_many :awardsendclass Awards ActiveRecord::Bas
例如,在
class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base belongs_to :student end 以上应该是正确的用法,但是如果我们使用的话 class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base has_one :student end 以上是不是也可以将student.awards作为一系列Award对象,并将award.student作为学生对象作为奖励的接收者,所以工作方式与帖子顶部的方法相同? 解决方法
has_one用于一对一关系,而不是一对多关系.
正确使用has_one: class Student < ActiveRecord::Base has_one :id_card end class IdCard < ActiveRecord::Base belongs_to :student end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |