Ruby on rails 接受嵌套的属性,与Rails中的关系数据库逻辑相矛盾
RubyonRails模型逻辑与关系数据库逻辑冲突 在RubyonRails中,拥有Ruby on rails 接受嵌套的属性,与Rails中的关系数据库逻辑相矛盾,ruby-on-rails,ruby,relational-database,Ruby On Rails,Ruby,Relational Database,RubyonRails模型逻辑与关系数据库逻辑冲突 在RubyonRails中,拥有所属的的模型将在数据库中具有外键。因此,模型: class Student < ApplicationRecord belongs_to :university end class University < ApplicationRecord has_many :university end 到目前为止还不错 当我希望接受学生模型中的的_嵌套的_属性_,以引用\u form.htm
所属的
的模型将在数据库中具有外键。因此,模型:
class Student < ApplicationRecord
belongs_to :university
end
class University < ApplicationRecord
has_many :university
end
到目前为止还不错
当我希望接受学生模型中的
的_嵌套的_属性_,以引用\u form.html.erb
中的大学时,就会出现问题。为了使用它,宿主模型(在本例中为Student)必须有一个has_one
,而不是一个所属的。因此,它将成为:
class Student < ApplicationRecord
has_one :university
accepts_nested_attributes_for :university
end
而数据库将有力地:
Student
-id int (PK)
-name varchar
University
-id int (PK)
-name varchar
-Student_id int (FK)
这是错误的,因为大学和学生之间应该有一对多的关系,而不是相反
那么,有没有一种方法可以使用接受
的嵌套属性,而不必强制反转模型中的关系,从而扰乱关系数据库逻辑?接受
的嵌套属性
所以你可能会对你想成为哪一个孩子的父母有点混淆。如果使用嵌套属性将该学生设置为该大学的家长,则不会引用该大学,而是创建一个新的大学记录,或使用学生表单中使用的嵌套属性更新现有记录
One-to-one
Consider a Member model that has one Avatar:
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar
end
Enabling nested attributes on a one-to-one association allows you to create the member and avatar in one go:
params = { member: { name: 'Jack', avatar_attributes: { icon: 'smiling' } } }
member = Member.create(params[:member])
member.avatar.id # => 2
member.avatar.icon # => 'smiling'
一对一
考虑一个具有一个化身的成员模型:
类成员2
member.avatar.icon#=>“微笑”
因此,您只能为使用这些嵌套属性的每个学生更新/创建新的大学记录
以下是一对多:
One-to-many
Consider a member that has a number of posts:
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
end
You can now set or update attributes on the associated posts through an attribute hash for a member: include the key :posts_attributes with an array of hashes of post attributes as a value.
For each hash that does not have an id key a new record will be instantiated, unless the hash also contains a _destroy key that evaluates to true.
params = { member: {
name: 'joe', posts_attributes: [
{ title: 'Kari, the awesome Ruby documentation browser!' },
{ title: 'The egalitarian assumption of the modern citizen' },
{ title: '', _destroy: '1' } # this will be ignored
]
}}
member = Member.create(params[:member])
member.posts.length # => 2
member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
一对多
考虑一个具有多个职位的成员:
类成员2
member.posts.first.title#=>“Kari,很棒的Ruby文档浏览器!”
member.posts.second.title#=>“现代公民的平等主义假设”
您也可以在上面的一对多示例中看到这一点。在发布学生记录时,您是否希望更改/创建大学记录
如果你只是在寻找一个参考资料,那么在你的第一个例子中,你基本上已经有了这个参考资料,你可以稍微改变你的路线来吸引学生
然后,一旦嵌套了路由,就可以对部分表单做一些工作
所以,如果我不在这里,请描述一下为什么当你创建/更新一个学生时,你想创建/更新一所大学。如果那是你想做的
这将有助于其他人更好地了解您的上下文,并可能有助于其他人理解您使用嵌套属性的意图
例如,添加您的表单部分并解释您的目标
编辑:
但因为我不确定你的计划到底有什么帮助
另一个赌注听起来像是你想在更新学生的大学记录时改变他们。因此,你会让学生属于大学,大学有许多学生,但也有许多大学记录,学生有许多/有一个大学记录,记录同时属于这两个。
又名:
然后你可以让学生接受大学记录的嵌套属性。我发现我在解释我的疑问时犯了一个错误(这是我的第一个问题,我从错误中吸取了教训)
最终目标是了解:
当有一个属于而不是一个有一个或有多个时,我可以使用accepts\u nested\u attributes\u吗
因此,答案是肯定的:
class Student < ApplicationRecord
belongs_to :university
accepts_nested_attributes_for :university
end
我真的,真的,真的打错了我的问题,我为此道歉
谢谢你的回答。一所大学有很多学生。它没有很多大学。这个代码实际上是一致的吗?关于“大学将有一个属于:学生,而不是一个有很多:学生”,“学生将失去其对大学的引用”和“后者将有一个对学生的引用”为什么?你的协会太离谱了<代码>学生
应该属于:大学
和大学
应该有很多:学生
。外键也必须是university\u id
而不是university\u id
。阅读,以便了解基础知识,然后您可能已经准备好处理嵌套属性,这是一个相当高级的主题。但是,您似乎也不了解这里的“关系数据库逻辑”。如果在大学中放置student\u id
列,则一所大学只能有一名学生。如果在学生上放置university\u id
列,则一名学生只能属于一所大学,但一所大学可以有许多学生。@询问者max似乎知道坏的DB设计是坏的,他们询问如何在嵌套accepts\u时不获取它们,但它们是由于不必要的&wr而产生的
One-to-one
Consider a Member model that has one Avatar:
class Member < ActiveRecord::Base
has_one :avatar
accepts_nested_attributes_for :avatar
end
Enabling nested attributes on a one-to-one association allows you to create the member and avatar in one go:
params = { member: { name: 'Jack', avatar_attributes: { icon: 'smiling' } } }
member = Member.create(params[:member])
member.avatar.id # => 2
member.avatar.icon # => 'smiling'
One-to-many
Consider a member that has a number of posts:
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
end
You can now set or update attributes on the associated posts through an attribute hash for a member: include the key :posts_attributes with an array of hashes of post attributes as a value.
For each hash that does not have an id key a new record will be instantiated, unless the hash also contains a _destroy key that evaluates to true.
params = { member: {
name: 'joe', posts_attributes: [
{ title: 'Kari, the awesome Ruby documentation browser!' },
{ title: 'The egalitarian assumption of the modern citizen' },
{ title: '', _destroy: '1' } # this will be ignored
]
}}
member = Member.create(params[:member])
member.posts.length # => 2
member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
class Student < ApplicationRecord
belongs_to :university
accepts_nested_attributes_for :university
end
def new
@student = Student.new
@student.build_university
end