# Habtm - controlling with checkboxes UI

**URL:** <https://www.ruby-forum.com/t/habtm-controlling-with-checkboxes-ui/113660>\
**Category:** Rails\
**Created:** [September 3, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/habtm-controlling-with-checkboxes-ui/113660 "2007-09-03T16:38:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [September 3, 2007, 4:38pm UTC](https://www.ruby-forum.com/t/habtm-controlling-with-checkboxes-ui/113660/1 "2007-09-03T16:38:47Z")

</div>

I need to put an array of checkboxes on a user profile editing form to  
allow users to self-select which neighborhoods they are interested in  
from the available neighborhoods. But I can’t figure out the syntax to  
put in the view to make the neighborhood.id’s translate into actual  
Neighborhood objects that makes AR happy.

The error I am getting from the below code is “Neighborhood expected,  
got String”. The Neighborhood id’s are coming in just fine, but the  
update\_attributes() method just sees them as strings with no meaning.  
Which would be fine if it would just put them in the DB and not get all  
huffy…

# The list of checkboxes looks like this:

\<% for hood in @hoods %\>

 \<%=h hood.name %\> 
\<% end %\>
# here is the AccountController:

# 

# profile viewing or editing

def profile  
@user = current\_user  
@hoods = Neighborhood.find(:all, :order=\>:name)  
return unless request.post?

if @user.update\_attributes(params[:user])  
flash[:notice] = ‘your account was successfully updated.’  
redirect\_to :action =\> ‘profile’  
else  
render :action =\> ‘profile’  
end  
end

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :sale\_alerts, :class\_name =\> “Neighborhood”  
…  
end

Thanks a Bunch!  
Sam

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [September 3, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/habtm-controlling-with-checkboxes-ui/113660/2 "2007-09-03T19:45:20Z")

</div>

I had some problems in my view with the ids

I usally do this

tabel.id.to\_s

and maybe you gotta go through them and convert them to integer again

params[:user][:sale\_alerts].each do |i|  
params[:user][:sale\_alerts][i] = i.to\_i  
end

PS: nothing tested

---

<div class="post-metadata">

**Author:** ![sbeam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/258eb7/32.png) [@sbeam](https://www.ruby-forum.com/u/sbeam)\
**Post date:** [September 4, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/habtm-controlling-with-checkboxes-ui/113660/3 "2007-09-04T14:58:10Z")

</div>

On Monday 03 September 2007, Jamal S. wrote:

> ```
> params[:user][:sale_alerts][i] = i.to_i
> 
> ```
> 
> end

Hi, the problem isn’t with strings\<-\>int, it’s with row IDs -\> actual AR  
objects.

I ended up adding methods to the User model as such:

def sale\_alerts\_hood\_ids=(a)  
self.sale\_alerts = a.collect! { |id| Neighborhood.find(id) }  
end

def sale\_alerts\_hood\_ids  
return self.sale\_alerts.collect { |sa| sa.neighborhood\_id.to\_i }  
end

and if in the view I put

\<% for hood in @hoods %\>  
\<input type=“checkbox” name=“user[sale\_alerts\_hood\_ids][]”  
value="\<%= hood.id %\>" \<%= (@user.sale\_alerts\_hood\_ids.include?  
(hood.id))? ‘checked=“true”’ : ‘’ %\> /\>  
\<% end %\>

then it seems to work. But this does a lot of find() calls potentially,  
and seems like kind of a PITA. Having a HABTM with this kind of UI is  
pretty common, I wonder if there is a more elegant solution?

Or maybe, this is sorta elegant. Maybe there is some way to DRY it up  
though in case I need to use this UI with a different combo of objects.  
But that’s outside my Rails comfort zone for now.
