# Checkboxes with a has\_many :through relation

**URL:** <https://www.ruby-forum.com/t/checkboxes-with-a-has-many-through-relation/97969>\
**Category:** Rails\
**Created:** [July 11, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/checkboxes-with-a-has-many-through-relation/97969 "2006-07-11T18:48:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 11, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/checkboxes-with-a-has-many-through-relation/97969/1 "2006-07-11T18:48:39Z")

</div>

All,

I’m populating a view with checkboxes in a list. I want to be able to  
assign to my has\_many relationship using the post values of these  
checkboxes. Essentially, I want to populate the relationship table  
which will create the correct join values in my model.

Here is my check\_box\_tag code:

\<%= check\_box\_tag("@current\_job[target\_list\_ids][]",  
tlist.DataSetID,  
@current\_job.target\_lists.include?(tlist)) %\>

So, in this example, tlist is an element of another array that I’m  
starting with to give me the list of choices. But what I want is to  
assign these choices into the “target\_lists” collection of my  
@current\_job object (again, via the “through” table based on my has\_many  
definition.

current\_job :has\_many target\_lists :through an association.

I first followed the examples of using checkboxes with HABTM. However,  
since I switched my model to user has\_many :through, and so I’m pretty  
sure that I no longer get the target\_lists\_ids method above like I would  
if I were using HABTM.

And actually, I’m not sure I care about that, since I know I should just  
be able to process through the values that I get from the form anyway.  
I’m having trouble getting at the values of the checkbox, even though in  
my parameter list, I see that they are available:

“@current\_job”=\>{“target\_list\_ids”=\>[“1”, “3”]}

There’s something very basic that I’m missing here. I want to be able  
to grab these values in my controller method and do something like:

params[@current\_job][:target\_list\_ids].each {  
//Do what’s necessary to link up my models via the has\_many :though  
relationship by populating the “through” table.  
}

Is my problem that I’m naming my checkboxes using  
@current\_job[target\_list\_ids], and this is being literally interpreted  
and ending up in the view that way?

I hope this makes sense. I think I am generally confused about how the  
check\_box and check\_box\_tag helpers work.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 15, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/checkboxes-with-a-has-many-through-relation/97969/2 "2006-07-15T22:48:30Z")

</div>

I have the same problem does anyone else have any suggestions on this?

Wes G. wrote:

> All,
> 
> I’m populating a view with checkboxes in a list. I want to be able to  
> assign to my has\_many relationship using the post values of these  
> checkboxes. Essentially, I want to populate the relationship table  
> which will create the correct join values in my model.

…snip…

> I hope this makes sense. I think I am generally confused about how the  
> check\_box and check\_box\_tag helpers work.
> 
> Thanks,  
> Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 16, 2006, 1:20am UTC](https://www.ruby-forum.com/t/checkboxes-with-a-has-many-through-relation/97969/3 "2006-07-16T01:20:47Z")

</div>

JoeWooten wrote:

> I have the same problem does anyone else have any suggestions on this?
> 
> Wes G. wrote:
> 
> > All,
> > 
> > I’m populating a view with checkboxes in a list. I want to be able to  
> > assign to my has\_many relationship using the post values of these  
> > checkboxes. Essentially, I want to populate the relationship table  
> > which will create the correct join values in my model.
> 
> …snip…
> 
> > I hope this makes sense. I think I am generally confused about how the  
> > check\_box and check\_box\_tag helpers work.
> > 
> > Thanks,  
> > Wes

Joe,

I got this working but don’t have time to post the code right now. Send  
an email to weyus at att dot net and please remind me to post my  
solution.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 16, 2006, 2:37am UTC](https://www.ruby-forum.com/t/checkboxes-with-a-has-many-through-relation/97969/4 "2006-07-16T02:37:03Z")

</div>

On this day, 16-July-2006 4:48 AM, JoeWooten wrote:

> …snip…
> 
> > I hope this makes sense. I think I am generally confused about how the  
> > check\_box and check\_box\_tag helpers work.
> > 
> > Thanks,  
> > Wes

Found these links.  
[jrhicks.net](http://jrhicks.net/96) → checkout the pdf  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/CheckboxHABTM)  
[Peak Obsession](http://wiki.rubyonrails.com/rails/pages/has_and_belongs_to_many) —\> the  
main page

P.V.Anthony

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 17, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/checkboxes-with-a-has-many-through-relation/97969/5 "2006-07-17T17:43:13Z")

</div>

Joe,

Here’s what I ended up doing…

Job and TargetList are related through their association object.  
Basically, in the form, I’m manipulating indices of an array that I then  
use to populate the “through” object. So I’m not attempting to  
manipulate the “child” or “parent” model directly.

Hope this helps,  
Wes

Job model:

has\_many :job\_list\_associations, :foreign\_key =\> :JobReferenceNumber  
has\_many :target\_lists, :through =\> :job\_list\_associations, :source =\>  
:target\_list

TargetList model:

has\_many :job\_list\_associations, :foreign\_key =\> :DataSetID  
has\_many :jobs, :through =\> :job\_list\_associations, :source =\> :job

Job model:

public  
def target\_list\_ids  
ids = Array.new  
unless target\_lists.nil?  
target\_lists.each do |list|  
ids \<\< list.DataSetID  
end  
end  
ids  
end

public  
def target\_list\_ids=(list\_array)  
list\_array.each do |id|  
target\_lists \<\< TargetList.find(id)  
end  
end

View:

 \<%= check\_box\_tag("current\_job[target\_list\_ids][]", tlist.DataSetID, @current\_job.target\_lists.include?(tlist), :class =\> 'list-data') %\> \<%= tlist.Alias %\>

Controller calls this method in the job model to manage the  
associations:

public  
def set\_list\_relationships(selected\_lists)  
#Find all of the existing associations before we save anything  
current\_job\_lists = Array.new  
JobListAssociation.find\_all\_by\_JobReferenceNumber(self.JobReferenceNumber,  
:select =\> ‘DataSetID’).each { |jla|  
current\_job\_lists \<\< jla.DataSetID  
}

```
#For each submitted list identifier...
selected_lists.each { |value|
  #...add the association if it doesn't exist and record that it was 

```

submitted  
unless current\_job\_lists.include?(value)  
jla = JobListAssociation.new  
jla.JobReferenceNumber = self.JobReferenceNumber  
jla.DataSetID = value  
jla.FaxDataTable = TargetList.find(value).Alias  
jla.save!  
end  
}

```
#Finally, remove all of the lists that were already stored but were 

```

not selected this time around.  
(current\_job\_lists - selected\_lists).each { |value|  
JobListAssociation.destroy\_all(“JobReferenceNumber =  
#{self.JobReferenceNumber} AND DataSetId = #{value}”)  
}  
end
