I'm trying to create a record within a join table from the action of a button. To explain, I would have an events model and would like to track selected events from each user.
I used the HABTM relationship since I dont really need any extra fields.
User.rb => has_to_and_belongs_to_many :events Event.rb => has_to_and_belongs_to_many :users Events_Users Migration => [user_id, event_id, id=>false]
I'm getting stuck on the actually creation of the record.. Someone helped me earlier with adding the record in within the console with worker great.
u = User.find(1)
u.events << Event.find(1)
Now I would like to perform the action as a result of clicking a link... Is this in the right direction?
def add
@user = User.find(session[:user_id])
@event = Event.find(params[:id])
if @user.events.save(params[:user][:event])
flash[:notice] = 'Event was saved.'
end
end
Should I add a @user.events.new somewhere and if so where do I put the params of which user and which event? Ayuda me por favor.