# Nested attributes with required protected attributes

**URL:** <https://www.ruby-forum.com/t/nested-attributes-with-required-protected-attributes/185814>\
**Category:** Rails\
**Created:** [March 26, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/nested-attributes-with-required-protected-attributes/185814 "2010-03-26T16:48:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 26, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/nested-attributes-with-required-protected-attributes/185814/1 "2010-03-26T16:48:25Z")

</div>

Hi,

I’ve got 3 models: Comment, Venue and User. Comment has many :venues  
and belongs to :user. Venue belongs to :user as well.

When adding a comment, user should be able to attach a venue to it.

In Venue model “user\_id” attribute is protected and it is required as  
well - this causes problem when trying to create a venue when adding a  
comment using nested attributes, because I can’t pass “user\_id”  
attribute required by venue - it’s protected and thus rejected.

Any ideas how to solve it?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 27, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/nested-attributes-with-required-protected-attributes/185814/2 "2010-03-27T21:27:56Z")

</div>

Hi Szimek,

You can do it like this:

# hash to simulate what comes from your params[:comment]

comment = { :text =\> “Comment text”, :venues\_attributes =\> [{ :name =\>  
“one”  
}, { :name =\> “two” }] }

# you set the user id by calling a block on create

Comment.create(model) { |c| c.venues.each { |v| v.user\_id = 1 } }

Hope this helps.

/Lasse

2010/3/26 szimek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 27, 2010, 9:30pm UTC](https://www.ruby-forum.com/t/nested-attributes-with-required-protected-attributes/185814/3 "2010-03-27T21:30:17Z")

</div>

Whoops,

Comment.create(model)

should of course be

Comment.create(comment)

And you should be able to replace it with

Comment.create(params[:comment])

/Lasse

2010/3/27 Lasse B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
