I have a join model in a HABTM relationship with a through association (details below). I ‘m trying to find a record….find the value of that records attribute…change the value and update the record but am having a hard time doing it.
The model setup is this>>
User.rb
has_many :choices
has_many :interests, :through => :choices
Interest.rb
has_many :choices
has_many :users, :through => :choices
Choice.rb
belongs_to :user
belongs_to :interest
and Choice has the user_id, interest_id, score as fields.
And I find the ?object? like so >>
@choice = Choice.where(:user_id => @user.id, :interest_id => interest.id)
So the model Choice has an attribute called :score. How do I find the value of the score column….and +1/-1 it and then resave?
I tried
@choice.score = @choice.score + 1
@choice.update_attributes(params[:choice])
flash[:notice] = "Successfully updated choices value."
but I get “undefined method score”……What did i miss?
You probably need to use
.firstto run the actual query and return a result:… otherwise
@choiceonly holds an Active Relation object… it hasn’t executed the query yet.