nop*_*ole 5 activerecord ruby-on-rails has-one belongs-to
例如,在
class Student < ActiveRecord::Base
has_many :awards
end
class Awards < ActiveRecord::Base
belongs_to :student
end
Run Code Online (Sandbox Code Playgroud)
以上应该是正确的用法,但是如果我们使用的话
class Student < ActiveRecord::Base
has_many :awards
end
class Awards < ActiveRecord::Base
has_one :student
end
Run Code Online (Sandbox Code Playgroud)
上面的内容也不能student.awards作为一个奖励对象的数组,award.student作为奖励的接收者的学生对象,所以工作方式与帖子顶部的方法相同吗?
has_one 用于一对一关系,而不是一对多关系.
正确使用has_one:
class Student < ActiveRecord::Base
has_one :id_card
end
class IdCard < ActiveRecord::Base
belongs_to :student
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3448 次 |
| 最近记录: |