How do you update HABTM relationships or create.

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.

Hi

How do u create form here?

Sijo

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 %>

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.

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,

Formtastic indeed.