Rails - How do i update a records value in a join model ?

Posted by ChrisWesAllen on Stack Overflow See other posts from Stack Overflow or by ChrisWesAllen
Published on 2011-01-07T02:38:05Z Indexed on 2011/01/07 2:54 UTC
Read the original article Hit count: 273

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about update