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 9256601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:58:59+00:00 2026-06-18T11:58:59+00:00

I have 2 models like the following Class Post has_many :comments, :dependent => :destroy

  • 0

I have 2 models like the following


    Class Post  
      has_many :comments, :dependent => :destroy 
    end  

    Class Comment  
      validates_presence_of :post
      validates_presence_of :comment
      belongs_to :post  
    end  
  

In Comments controller,


    def create
      comment = @post.comments.build(params[:comment])
      if comment.save
        // some code
      else
        // some code
      end
    end

When the comment is invalid as per the validation, the comment is not saved. But when the @post object is accessed in the view, it contains a comment object with nil id. This did not happen in Rails 2.3.11. We are upgraded to Rails 3.1 and then now to Rails 3.2. This comment object with nil id disappears when I do @post.reload. We are using REE.

I tried to interchange build and new methods. It had the same result as build. Similar behavior is found across our application. Is it the expected behavior or am I doing something wrong?

  • 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-18T11:59:00+00:00Added an answer on June 18, 2026 at 11:59 am

    This seems like expected behaviour to me.

    via http://guides.rubyonrails.org/association_basics.html#belongs_to-association-reference

    4.1.1.3 build_association(attributes = {})

    The build_association method returns a new object of the associated
    type. This object will be instantiated from the passed attributes, and
    the link through this object’s foreign key will be set, but the
    associated object will not yet be saved.

    When you call @post.comments.build(...), Rails:

    1. Creates a new Comment object
    2. sets comment.post_id to @post.id.
    3. Inserts it into the comments array (in memory).

    When the validation fails, it doesn’t delete the comment and the comment persists in the in-memory comments array. When @post gets to your view, @post.comments still includes that badly validated comment.

    As for how to deal with it, I’m not sure. Maybe you could do something like (in your controller)… (Feels pretty ugly though.)

    def create
      comment = @post.comments.build(params[:comment])
      if comment.save
        // some code
      else
        @bad_comment = @post.comments.pop
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class Post < ActiveRecord::Base has_many :responses, as: :responseable, dependent:
I have the following models. class Post < ActiveRecord::Base has_many :timeline_items, :as=>:subject define_index do
I have a rails 3.1 app with the following Models: class Post < ActiveRecord::Base
I have 3 model, User, Post, Comment with definition like below class Post <
I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I have a model like the following one: public class TestModel{ public IList<Field> Fields
I have models like this: class Vendor(models.Model): title = models.CharField() class Product(models.Model): ... vendor
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model): nazwa =
So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):

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.