Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:29:45+00:00 2026-06-03T09:29:45+00:00

I have a Post has_many Comments association. Post has boolean attribute published . When

  • 0

I have a Post has_many Comments association. Post has boolean attribute published.
When post.published is false, the new comment shouldn’t be valid.
What is the best practise to accomplish this kind of validation?

I’ve tried to do it by this way, but sadly, it doesn’t work correctly. It is still possible to create new comment for unpublished post.

class Comment < ActiveRecord::Base
  validates :post_id, presence: true, if: :post_is_published

  ...
  def post_is_publised
    post && post.published
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:29:46+00:00Added an answer on June 3, 2026 at 9:29 am

    Hmm.. I think you have syntax errors in your code… Try this:

    class Comment < ActiveRecord::Base
      validates :post_id, :presence => true, :if => :post_is_published
    
      def post_is_publised
        post.try(:published)
      end
    end
    

    After reading your console output and checking your question one more time:

    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
    

    I understand that you don’t want to allow adding comments to unpublished posts. Above code should accomplish that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, post and comment. Posts has many comments and comments belongs
Suppose we have two tables. Post and Comment. Post has many Comments. Pretend they
Using this as an example contexted: Post has_many comments Comment belongs_to post I have
I have a simple polymorphic association #comment.rb belongs_to :commentable, :polymorphic => true has_many :comments,
I have a post which has many comments, and I want to display the
I have a posts controller and a comments controller. Post has many comments, and
I have a post controller that has many comments. The post model has a
I have a a Comments table and a Posts table, where a Post has
I have a Post model that has a polymorphic association with a Vote model:
I have three models: Post ( has_many votes as votable ), Comment ( has_many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.