无法使用auto_save:true在rails中保存关联模型

时间:2017-10-05 11:50:12

标签: rails-activerecord

请考虑以下代码段。

我有一个名为Post的父模型:

class Post < ActiveRecord::Base
  has_many :comments, dependent: :destroy, autosave: true
  accepts_nested_attributes_for :comments, reject_if: :all_blank, allow_destroy: true, update_only: true
end

再次在我提到的相关评论模型中:

class Comment < ActiveRecord::Base
  belongs_to :post
end

所以,当我尝试为帖子创建评论时:

post  = Post.first
comment = post.comment.new
comment.description = "Some Text"
post.save

只保存帖子。相反,评论虽然在关联中启用了自动保存,但并未寻求任何插入。

据我所知,上述事情必须奏效。但是,它不能正常工作。

如果我错了,请纠正我。

0 个答案:

没有答案