# How do you update HABTM relationships or create

**URL:** <https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700>\
**Category:** Rails\
**Created:** [June 5, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700 "2010-06-05T16:10:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 5, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700/1 "2010-06-05T16:10:50Z")

</div>

Hi,

I have a User model with a HABTM relation to a Role model and vice  
versa. So There’s a roles\_users table that references :user and :role.

I’m using formtastic, and in the user.edit.html.erb I have a line that  
outputs the roles as a multi-select list box. But the box is not  
showing the roles that have been assigned to the users.

Another problem is that if I select a few roles and press the update  
button, the roles are not being asssigned. I have to manually assign  
roles by accessing the database.

Isn’t this stuff supposed to happen automatically when you call  
@user.update\_attributes(params[:user]) in the user\_controller ?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 5, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700/2 "2010-06-05T19:31:12Z")

</div>

Hi

How do u create form here?

Sijo

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 5, 2010, 11:45pm UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700/3 "2010-06-05T23:45:36Z")

</div>

Hi, just like the following:

\<% semantic\_form\_for @user do |f| %\>  
\<% f.inputs do %\>  
\<%= f.input :email %\>  
\<%= f.input :first\_name %\>  
\<%= f.input :last\_name %\>  
\<%= f.input :roles %\>  
\<% end %\>  
\<%= f.buttons %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 5, 2010, 11:59pm UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700/4 "2010-06-05T23:59:40Z")

</div>

Ok I found the problem, I had an attr\_accessor :role\_ids in my User  
model  
which was throwing things off. Everything worked after I removed it.

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 7, 2010, 6:21am UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700/5 "2010-06-07T06:21:06Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am very new to Rails and I am more or less only 6 months to 1 year  
> kid in Rails…
> 
> I just wondering with your code… I really dont see what is  
> "semantic\_form\_for " and f.buttons?? Is it view helper that you have  
> written or any plugin which overrides the default form\_for construct??  
> This would be very helpful to me in writing the more maintainable  
> Rails code…

He said he was using Formtastic. That’s probably where those methods  
come from. But don’t take my word for it. Google is your friend in  
this case.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 8, 2010, 2:17am UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700/6 "2010-06-08T02:17:07Z")

</div>

Formtastic indeed.

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [June 7, 2010, 6:07am UTC](https://www.ruby-forum.com/t/how-do-you-update-habtm-relationships-or-create/189700/7 "2010-06-07T06:07:34Z")

</div>

I am very new to Rails and I am more or less only 6 months to 1 year  
kid in Rails…

I just wondering with your code… I really dont see what is  
"semantic\_form\_for " and f.buttons?? Is it view helper that you have  
written or any plugin which overrides the default form\_for construct??  
This would be very helpful to me in writing the more maintainable  
Rails code…
