Has_and_belongs_to_many form with check_box

Hi,

I have 2 models, user and project.
When I edit my profile I wan’t to check the projects I wan’t to follow.
So here is my code:

<% form_for @user do |f| %>

<% for project in @projects %>
<% fields_for “user[project_attributes][]”, project do
|project_form| %>


  • <%= project_form.check_box “checked”, {:checked =>
    @user.project_ids.include?(project.id)} %><%= project.name
    %>

  • <% end %>
    <% end %>

    <% end %>

    I think the form is correct but the model and controller part is not
    very clear…

    Here is the log:
    “user”=>{“project_attributes”=>{“1”=>{“checked”=>“1”},
    “2”=>{“checked”=>“1”}, “3”=>{“checked”=>“0”}, “4”=>{“checked”=>“0”},
    “5”=>{“checked”=>“0”}}, “password_confirmation”=>"[FILTERED]",
    “password”=>"[FILTERED]"

    The project_attributes is really messy and I have to parse the hash to
    get selected projects.

    There must be a cleaner way??

    Greg

    You seen this?

    http://asciicasts.com/episodes/17-habtm-checkboxes

    Seems like you would want to use something like:
    @product.categories.include?(category)

    except you would be like @user.projects.include?(project)