# Adding a selected checkbox to a new nested model without polluting the model

**URL:** <https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631>\
**Category:** Rails\
**Created:** [September 8, 2010, 7:58am UTC](https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631 "2010-09-08T07:58:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gamov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gamov](https://www.ruby-forum.com/u/gamov)\
**Post date:** [September 8, 2010, 7:58am UTC](https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631/1 "2010-09-08T07:58:39Z")

</div>

Context:  
I have a GoodsReturn model with many ReturnedItem. For the new view, I  
create a new GoodsReturn with many new ReturnedItems from a sale (and  
its SoldItems).  
Problem:  
I want to have a Add/Selected checkbox with which the user can pick  
the items he wants to return. I know I could use a ‘selected’ virtual  
attribute but I don’t want to pollute my model just for this. In fact,  
there is something that does exactly the opposite that I want,  
the :\_destroy:  
\<%= i\_f.check\_box :\_destroy %\>  
I would like exactly the opposite, :\_select.

Anybody has figured this out before?

Thanks guys,  
Gam

---

<div class="post-metadata">

**Author:** ![gamov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gamov](https://www.ruby-forum.com/u/gamov)\
**Post date:** [September 8, 2010, 5:08pm UTC](https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631/2 "2010-09-08T17:08:25Z")

</div>

sure is simple, i will tell you what i think you are trying to say and  
you  
can confirm it,  
you have a list of items, and you want the user to be able to make a  
selection and remove them with checkboxes?  
is that it?

---

<div class="post-metadata">

**Author:** ![gamov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gamov](https://www.ruby-forum.com/u/gamov)\
**Post date:** [September 9, 2010, 1:08pm UTC](https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631/3 "2010-09-09T13:08:31Z")

</div>

i think i saw something similar here

> **[\#196 Nested Model Form Part 1 - RailsCasts](http://railscasts.com/episodes/196-nested-model-form-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#197 Nested Model Form Part 2 - RailsCasts](http://railscasts.com/episodes/197-nested-model-form-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

it has ajax and eveything

---

<div class="post-metadata">

**Author:** ![gamov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gamov](https://www.ruby-forum.com/u/gamov)\
**Post date:** [September 9, 2010, 6:39am UTC](https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631/4 "2010-09-09T06:39:49Z")

</div>

Cool, radhames brito.

I have a list of items (all are new\_record? = true) that are nested  
into another new record. I want the user to make a selection and ADD  
them with checkboxes, not remove. Those without checkboxes will just  
be discarded and never created.

---

<div class="post-metadata">

**Author:** ![gamov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gamov](https://www.ruby-forum.com/u/gamov)\
**Post date:** [September 15, 2010, 9:20am UTC](https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631/5 "2010-09-15T09:20:42Z")

</div>

Yeah, it’s quite similar, but the example uses the \_destroy facility,  
I want the opposite…  
Anyway, I’m using a virtual attribute in the meantime…

---

<div class="post-metadata">

**Author:** ![gamov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gamov](https://www.ruby-forum.com/u/gamov)\
**Post date:** [September 15, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/adding-a-selected-checkbox-to-a-new-nested-model-without-polluting-the-model/194631/6 "2010-09-15T14:06:58Z")

</div>

oh i see, you can handle the way the hash is passed so that you can loop  
true the params and check for the selected one, then  
only save the selected ones , i did something similar. You can handle  
the  
hash by adding [] to the field names in the for helper.  
for example addin :value[whatever][] associates the items and puts  
then  
in an array in the params hash
