通过关联构建和创建相关对象:如何设置嵌套模型的外键?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用:
您可以在此处查看更多信息,并且可能想要看看嵌套属性以及 validates_linked
try using :
you can see here for more info, and might want to have a look at nested attributes and at validates_associated
在我看来,你就是不能那样做。
article.valid?
必须为 true 才能保存任何记录。因此,只要您需要同时CREATE新文章及其子对象,
article.valid?
将始终为 false您可以做什么
In my opinion, you just CAN'T do that.
article.valid?
must be true before you can save any record.Therefore,
article.valid?
will always be false as long as you need to CREATE new article and its sub-objects at the same timeWhat you can do