I have events and users/teams.
class Event
has_many :users, :through => :registrations
end
class User
has_many :events, :through => :registrations
end
class Registration
belongs_to :users
belongs_to :events
end
When I register a user, I’m connecting them to the event like so:
@event.users << @user
Does this implicitly create the Registration object for the user/event? I’ve put a :goal_amount column in my Registration migration, and I would like to be able to set the :goal_amount when the Registration is created. Do I need to explicitly create a Registration (ie: Registration.create(:user_id => @user.id, :event_id => @event.id, :goal_amount => params[:goal_amount]) to make this happen?
Yes, adding a user to an event automatically creates the relation object.
And yes you must manually create the relation if you wish to add that parameter in the middle table.
One solution to make it look cooler would be to create a
add_usermethod in the event object.Then you just have to call