我在與直通關聯的HABTM關係中有一個連接模型(詳情如下)。我試圖找到一條記錄....找到該記錄屬性的值...更改值並更新記錄,但是很難做到這一點。Rails - 如何更新連接模型中的記錄值?
模型設置是這樣的>>
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
,並選擇有USER_ID ,interest_id,分數爲字段。
我找到了?對象?像這樣>>
@choice = Choice.where(:user_id => @user.id, :interest_id => interest.id)
所以模型Choice有一個屬性叫做score。如何找到評分欄的值....和+ 1/-1然後重新保存?
我試圖
@choice.score = @choice.score + 1
@choice.update_attributes(params[:choice])
flash[:notice] = "Successfully updated choices value."
,但我得到 「未定義的方法得分」 ......我錯過了什麼?
檢查!可能是你正在獲取@choice數組。 – 2011-01-07 02:44:29