ruby on rails - Add validation in acts_as_commentable gem -
i'm using gem called acts_as_commentable
i have added custom column in migration like: recipient_id
then generated comment model per documentation:
rails g comment
now, in comment.rb, have following line:
validate :comment, :recipient_id, :presence => true
note: comment
column added gem itself
still, after following documentation, when fire following deliberately:
commentable = post.create(:title => ......) comment = commentable.comments.create comment.comment = "some comment" comment.recipient_id = nil comment.save!
the comment object seems like:
<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">
why it's not validating presence of recipient_id
?
your calling validate
instead of validates
. both different.
it should be:
validates :comment, :recipient_id, :presence => true
Comments
Post a Comment