0

我正在使用一个名为act_as_commentable的 gem

我在迁移中添加了一个自定义列,例如:recipient_id

然后我根据文档生成了一个Comment模型:

rails g comment

现在,在我的Comment.rb中,我有以下行:

validate :comment, :recipient_id, :presence => true

注意:comment列已由 gem 本身添加

尽管如此,在遵循文档之后,当我故意触发以下内容时:

commentable = Post.create(:title => ......)
comment = commentable.comments.create
comment.comment = "Some comment"
comment.recipient_id = nil
comment.save!

评论对象看起来像:

<Comment id: 1, comment: "Some comment", commentable_id: 1, commentable_type: "Post", recipient_id: nil, created_at: "2015-06-13 09:41:23", updated_at: "2015-06-13 09:41:23">

为什么它不验证存在recipient_id

4

1 回答 1

1

你打电话validate而不是validates. 他们都是不同的。

它应该是:

validates :comment, :recipient_id, :presence => true
于 2015-06-13T11:02:01.217 回答