I have a model Project
which has many ProjectGenres
which references Genres
:
class Project < ActiveRecord::Base
has_many :project_genres
accepts_nested_attributes_for :project_genres, allow_destroy: true
has_many :genres, through: :project_genres
end
class ProjectGenre < ActiveRecord::Base
belongs_to :project
belongs_to :genre
end
class Genre < ActiveRecord::Base
has_many :project_genres
has_many :projects, through: :project_genres
end
When I create a project, I want to tick the appropriate genres it affiliates with. Then when they submit this form, it should create the appropriate records in the ProjectGenre
table.
I have the following line in my form:
<%= f.collection_check_boxes(:project_genres, Genre.all, :id, :description) %>
I'm not really sure what I do in the controller though? I get an array passed back in params[:project][:project_genres]
(although it passes back an extra empty entry), but am I supposed to do the donkey work here in creating the embedded objects myself, or am I missing something to be able to just create these automatically?