通过关联建立和创建相关对象:如何设置嵌套模型的外键? [英] Building and creating related objects via the association: how to set the foreign key of a nested model?

查看:86
本文介绍了通过关联建立和创建相关对象:如何设置嵌套模型的外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby on Rails 3.1.0.我正在尝试保存具有属性的嵌套模型,该属性旨在存储父模型的外键.在创建父模型时,我想将该属性值设置为父模型id值.

I am using Ruby on Rails 3.1.0. I am trying to save a nested model having an attribute that is intended to store the foreign key of the parent model. At the creation time of the parent model I would like to set that attribute value to the parent model id value.

在我的模型中,我有:

class Article < ActiveRecord::Base  
  has_many :article_category_relationships
  has_many :categories,
    :through => :article_category_relationships

  # Accept nested model attributes
  accepts_nested_attributes_for :articles_category_relationships
end

class Category < ActiveRecord::Base
  has_many   :article_category_relationships
  has_many   :articles,
    :through => :article_category_relationships
end

# The join model:
class ArticleCategoryRelationship < ActiveRecord::Base
  # Table columns are:
  #   - article_id
  #   - category_id
  #   - user_id
  #   - ...

  belongs_to :category
  belongs_to :article
end

在我看来,我有以下内容:

In my view I have the following:

...

<% @current_user.article_categories.each do |article_category| %>
  <%= check_box_tag 'article[articles_category_relationships_attributes][][category_id]', article_category.id, false %>
<% end %>

在我的控制器中,我有:

In my controller I have:

def create
  @article = Article.new(params[:article])

  ...
end

在我的情况下,article_id(与ArticleCategoryRelationship嵌套模型有关)应该在创建Article之后设置为@article.id值,并且问题在于Ruby on Rails框架似乎简而言之,考虑到我的情况,我想自动附加外键.

In my case, the article_id (related to the ArticleCategoryRelationship nested model) should be set to the @article.id value after the Article creation and the problem is that the Ruby on Rails framework seems do not set that value at the creation time. In few words, considering my case, I would like to attach the foreign key automatically.

只需知道,提交表单时生成的params是:

Just to know, the generated params when the form is submitted is:

"article"=>{"title"=>"Sample title", "articles_category_relationships_attributes"=>[{"category_id"=>"8"}, {"category_id"=>"9"}, {"category_id"=>"10"}] }

是否可以自动"设置嵌套模型的外键(article_id)?如果是这样,我该怎么办?

推荐答案

我认为您无法做到这一点.

  • 如果要保存article_category_relationships,则需要为它们每个保存一个article_id.
  • 并且,当您保存文章时,r​​ails将首先验证文章和所有子对象.这表示 article.valid?必须为true,然后才能保存任何记录.
  • 由于在保存到数据库之前article没有ID,所以article_category_relationships中的任何article_id为空. 因此,只要您需要同时创建新文章及其子对象
  • article.valid?始终为false.
  • 总结一下,这是rails的步骤:

    In my opinion, you just CAN'T do that.

    • If you want to save articles_category_relationships, you need a article_id for each of them.
    • And, when you save article, rails will first validate article and all sub-objects. This means article.valid? must be true before you can save any record.
    • Since article does not have an id before save to db, article_id in any of articles_category_relationships is empty. Therefore, article.valid? will always be false as long as you need to CREATE new article and its sub-objects at the same time
    • To summarize, here is the steps of rails:

      1. 验证文章本身,成功!(注意:笔记已保存)
      2. 为每个article.articles_category_relationships验证articles_category_relationship,未提供article_id,失败!

      1. 首先创建文章
      2. 将参数分配给此创建的文章
      3. 使用参数更新

      这篇关于通过关联建立和创建相关对象:如何设置嵌套模型的外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆