Has_and_belongs_to_many form with check_box


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:
    “2”=>{“checked”=>“1”}, “3”=>{“checked”=>“0”}, “4”=>{“checked”=>“0”},
    “5”=>{“checked”=>“0”}}, “password_confirmation”=>"[FILTERED]",

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

    There must be a cleaner way??


    You seen this?


    Seems like you would want to use something like:

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