How can I use the ActiveRecord accepts_nested_attributes_for helper in has_many: through association when adding attributes to the connection table?
For example, let's say I have a Team model:
class Team < ActiveRecord::Base
role = Role.find_by_name('player')
has_many :players,
:through => :interactions,
:source => :user,
:conditions => ["interactions.role_id = ?", role.id] do
class_eval do
define_method("<<") do |r|
Interaction.send(:with_scope, :create => {:role_id => role.id}) { self.concat r }
end
end
end
end
The has_many command is playersthrough interactions, because the user can take several roles (player, manager, etc.).
How can I use accepts_nested_attributes_for while adding attributes to the connection table?
If I have an existing command entry teamand an existing user entry user, I can do something like this:
team.players << user
team.players.size
=> 1
But if I create a new team with an embedded player:
team = Team.create(:name => "New York Lions",
:players_attributes => [{:name => 'John Doe'}])
team.players.size
=> 0
, ( ), interactions.role_id .