
Untitled
By: a guest on
Jul 29th, 2012 | syntax:
None | size: 0.58 KB | hits: 15 | expires: Never
Custom validation in Rails
class Comment < ActiveRecord::Base
validates :post_id, presence: true, if: :post_is_published
...
def post_is_publised
post && post.published
end
end
class Comment < ActiveRecord::Base
validates :post_id, :presence => true, :if => :post_is_published
def post_is_publised
post.try(:published)
end
end
class Comment < ActiveRecord::Base
validate :post_has_to_be_published
def post_has_to_be_published
unless post.try(:published)
self.errors.add(:base, "you can add comments only to published posts")
end
end
end