【发布时间】:2011-01-07 18:11:44
【问题描述】:
我在 HABTM 关系中有一个连接模型,具有直通关联(详情如下)。我正在尝试查找记录....找到该记录属性的值...更改值并更新记录,但很难做到这一点。
模型设置是这个>>
用户.rb
has_many :choices
has_many :interests, :through => :choices
利息.rb
has_many :choices
has_many :users, :through => :choices
Choice.rb
belongs_to :user
belongs_to :interest
Choice 有 user_id、interest_id、score 作为字段。
然后我找到了 ?object?像这样>>
@choice = Choice.where(:user_id => @user.id, :interest_id => interest.id)
所以模型 Choice 有一个名为 :score 的属性。如何找到 score 列的值....并将其 +1/-1 然后重新保存?
我试过了
@choice.score = @choice.score + 1
@choice.update_attributes(params[:choice])
flash[:notice] = "Successfully updated choices value."
但我得到“未定义的方法分数”......我错过了什么?
【问题讨论】:
-
检查!可能是你得到了@choice 的数组。
标签: ruby-on-rails