6

I have a Post model. I would like to let users create post comments while creating/updating a post by accepts_nested_attributes_for :comments. However, I don't want to let users update comments by nested attributes.

Is there a way to do something like accepts_nested_attributes_for :comments, create_only: true?

1
  • Pretty interesting question. Commented Jul 28, 2014 at 14:32

2 Answers 2

3

Try to use something like this accepts_nested_attributes_for :comments, reject: :persisted? This will check, if Post object persisted or not. You also can replace persisted? with your custom method

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, but I think you misread my question. I didn't mean to reject nested attributes for comments when the post is persisted. What I meant is only accepting nested attributes for creating comments.
Sorry,it was my mistake. But I think you can use method for reject_if which will check if comments_attributes have :id params or not.
Yeah, that what I was doing.
0

You can reject_if the id attribute is present:

accepts_nested_attributes_for :comments, reject_if: proc { |attributes| attributes['id'].present? }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.