I have a Join Model I just Migrated called EventUsers that has an user_id, event_id, and an attribute i created called opinion:string
create_table :events_users, :id => false do |t|
t.integer :user_id
t.integer :event_id
t.string :opinion # extra attribute I made
end
and
event.rb
has_and_belongs_to_many :users
and user.rb
has_and_belongs_to_many :events
I can add to the join model with
@user = User.find(session[:user_id])
@event = Event.find(params[:id])
@user.events << @event
but before I do that…… How do I set the opinion attribute to some string and then insert?
If your join model has any fields other than just the foreign keys, then you shouldn’t be using a HABTM relationship. You should create an intermediate model such as Opinion and have each model related like so:
Update:
In order to add opinions, you will likely want to create a RESTful interface. I would start by adding a nested route within either users, events, or both
Then add an opinions controller with your standard RESTful actions (new, create, edit, update, etc) and corresponding views. There are tons of resources on how to build a standard restful interface, but I would start with these:
http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default
http://guides.rubyonrails.org/routing.html#nested-resources