Building and creating related objects via the association: how to set the foreign key of a nested model?

妖精的绣舞 提交于 2019-12-08 03:45:17

问题


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.

In my model I have:

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

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.

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"}] }

Is it possible to "auto"-set the foreign key (article_id) of the nested model? If so, how can I do that?


回答1:


try using :

@a_particular_article.article_category_relationships.build(params[:something])

you can see here for more info, and might want to have a look at nested attributes and at validates_associated




回答2:


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. validate article itself, success!(NOTE: note saved)
    2. validate articles_category_relationship for each of article.articles_category_relationships, article_id not provided, fail!

What you can do

  1. create article first
  2. assign params to this created article
  3. update with params


来源:https://stackoverflow.com/questions/7628262/building-and-creating-related-objects-via-the-association-how-to-set-the-foreig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!