通过关联构建和创建相关对象:如何设置嵌套模型的外键?

发布于 2024-12-07 23:16:53 字数 1764 浏览 3 评论 0原文

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

在我的模型中,我有:

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

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

...

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

在我的控制器中,我有:

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

  ...
end

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

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

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

Is it possible to "auto"-set theforeign key (article_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.

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?

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

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

发布评论

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

评论(2

骷髅 2024-12-14 23:16:53

尝试使用:

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

您可以在此处查看更多信息,并且可能想要看看嵌套属性以及 validates_linked

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

日裸衫吸 2024-12-14 23:16:53

在我看来,你就是不能那样做。

  • 如果您想保存articles_category_relationships,则需要为每个条目提供一个article_id。
  • 并且,当您保存文章时,r​​ails 将首先验证文章和所有子对象。这意味着
    article.valid? 必须为 true 才能保存任何记录。
  • 由于文章在保存到数据库之前没有 id,因此任何articles_category_relationships 中的article_id 为空。
    因此,只要您需要同时CREATE新文章及其子对象,article.valid?将始终为 false
  • 总结一下,以下是步骤:导轨:
    1. 验证文章本身,成功!(注意:注释已保存)
    2. 验证每个article.articles_category_relationships的articles_category_relationship,未提供article_id,失败!

您可以做什么

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

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