如何在 Ruby on Rails 3 的视图和控制器中利用模型关联?

发布于 2024-11-06 13:35:31 字数 955 浏览 6 评论 0原文

我是 Rails 新手,正在尝试设置我的模型,并且想知道 Rails 如何处理关联。

我有一个 Quest 对象,它“属于”或通过外键引用许多其他对象,包括用户和内容:

quest.user_id  
quest.a_different_name_id  #this is a foreign key to a Content object

这些都是分别引用用户对象和内容对象的外键。

用户和内容“has_many”任务。

我知道这个设置允许我做类似的事情:

u = User.create #saves to database  
u.quests.build  #creates new Quest object with user id set to u.id  

我可以在相反的方向做一些事情,比如:

form_for @quest do |f|
    f.text_field :a_user_attribute            #an attribute of a User object
    f.text_field :a_different_name_attribute  #an attribute of a Content object

表单有文本字段,用于 Quest 对象通过其外键引用的对象的属性,而不是有一个表单实际的外键,这样当在控制器中时,我有:

@quest = Quest.new(params[:quest])

Rails是否足够智能,可以“通过”模型定义的外键关系并填充,然后保存用户和内容对象,并在@quest中适当地将外键设置为引用新创建的对象?

即使 Content 对象的外键名称与 content_id 不同,它也可以执行此操作吗?

希望这是有道理的...如果我不清楚,请告诉我。

I am new to Rails and am trying to set up my Models and was wondering how Rails handles associations.

I have a Quest object which "belongs_to" or references via foreign keys a number of other objects, including User and Content:

quest.user_id  
quest.a_different_name_id  #this is a foreign key to a Content object

these are both foreign keys referencing a User object and Content object respectively.

Both User and Content "has_many" Quests.

I understand that this setup allows me to do things like:

u = User.create #saves to database  
u.quests.build  #creates new Quest object with user id set to u.id  

Can I do something in the opposite direction like:

form_for @quest do |f|
    f.text_field :a_user_attribute            #an attribute of a User object
    f.text_field :a_different_name_attribute  #an attribute of a Content object

where the form has text fields for the attributes of the objects which a Quest object references through its foreign keys as opposed to having a form for the actual foreign keys, so that when in the controller I have:

@quest = Quest.new(params[:quest])

Is Rails smart enough to "reach through" the model-defined foreign key relationships and populate and then save the User and Content objects and appropriately set the foreign keys in @quest to reference the newly created objects?

Can it do this even though the foreign key for the Content object has a different name than content_id?

Hope this makes sense... let me know if I am being unclear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-11-13 13:35:31

您可以使用 Rails http://guides.rubyonrails 中的嵌套属性功能执行所需操作。 org/2_3_release_notes.html#nested-attributes

在此处查看表单助手
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html 基本上,

您需要执行以下操作:

class User < ActiveRecord::Base
  has_many :quests
  accepts_nested_attributes_for :quests
  ...
end

class Quest < ActiveRecord::Base
  belongs_to :user
  ...
end

然后在表单中执行以下操作:

<%= form_for @user do |f| %>
  UserAttrA  : <%= f.text_field :a_user_attribute_a %>
  UserAttrB: <%= f.text_field :a_user_attribute_b %>
  <%= f.fields_for :quests do |qf| %>
    QuestAttrA  : <%= qf.text_field :a_quest_attribute_a %>
    QuestAttrB: <%= qf.text_field :a_quest_attribute_b %>
  <% end %>
  UserAttrC  : <%= f.text_field :a_user_attribute_c %>
  UserAttrD: <%= f.text_field :a_user_attribute_d %>
<% end %>

并且您的控制器将像上面一样工作。

请注意,您可以在任务输入之前和/或之后显示用户输入。基本上,您可以使视图中的表单看起来像您想要的那样。但服务器上的语义需要保持一致。

You can do what you need with the Nested Attributes feature in Rails http://guides.rubyonrails.org/2_3_release_notes.html#nested-attributes

Check out the form helper for it here
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

Basically you would need to do the following:

class User < ActiveRecord::Base
  has_many :quests
  accepts_nested_attributes_for :quests
  ...
end

class Quest < ActiveRecord::Base
  belongs_to :user
  ...
end

then in the form you do the following:

<%= form_for @user do |f| %>
  UserAttrA  : <%= f.text_field :a_user_attribute_a %>
  UserAttrB: <%= f.text_field :a_user_attribute_b %>
  <%= f.fields_for :quests do |qf| %>
    QuestAttrA  : <%= qf.text_field :a_quest_attribute_a %>
    QuestAttrB: <%= qf.text_field :a_quest_attribute_b %>
  <% end %>
  UserAttrC  : <%= f.text_field :a_user_attribute_c %>
  UserAttrD: <%= f.text_field :a_user_attribute_d %>
<% end %>

And your controller would work just like you have above.

Note that you can display User inputs before and/or after Quest inputs. Basically you can make the form in the view look how you want. But the semantics on the server will be need to be consistent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文