If you have a normal connection with has_and_belongs_to_many , for example, with a movie and a participant, you can create a movie along with your examples.
As your joining model is more complex, you need to create separate roles separately:
writer= Roleship.create( participant: Participant.find_by_name('Spielberg'), role: Role.find_by_name('Director') ) main_actor= Roleship.create( participant: Participant.find_by_name('Willis'), role: Role.find_by_name('Actor') ) Film.create!(name: "Some film", roleships: [writer, main_actor])
To do this, all the attributes that you use to create roles and films must be assigned by the masses, so in Rails 3.2 you will need to write:
class Roleship < ActiveRecord::Base attr_accessible :participant, :role ... end class Film < ActiveRecord::Base attr_accessible :name, :roleships ... end
If you want to use the role__id role, you must write
class Film < ActiveRecord::Base attr_accessible :name, :roleship_ids ... end
Application:
Because of this, you can write the setter method
class Film ... def writers=(part_ids) writer_role=Role.find_by_name('Writer')
but this makes your code dependent on the data in your database (the contents of the roles table), which is a bad idea.
source share