麻烦更新has_many:通过记录

时间:2013-02-18 21:21:59

标签: ruby-on-rails has-many-through acts-as-list

我在更新has_many至记录时遇到问题。这是我的设置:

class TastingGroup < ActiveRecord::Base
  has_many :group_wine
  has_many :wines, through: :group_wine
end

class GroupWine < ActiveRecord::Base
  belongs_to :tasting_group
  belongs_to :wine
end

class Wine < ActiveRecord::Base      
  has_many :group_wine
  has_many :tasting_groups, through: :group_wine
end

我正在尝试使用acts_as_list,因为TastinGroup中葡萄酒的顺序很重要,所以我在GroupWine模型中添加了“position”属性。

但是,当我尝试更新GroupWine记录时,我收到以下错误,这就是我正在做的事情。

gw = GroupWine.first
#<GroupWine:0x007fd9f7c38b50> {
         :wine_id => 1,
         :tasting_group_id => 1,
         :position => nil
}

gw.position = 1
gw.save

这是我得到的错误......

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'group_wines.' in 'where clause': UPDATE `group_wines` SET `position` = 3 WHERE `group_wines`.`` IS NULL

group_wines的NULL是什么,为什么要添加where子句?

感谢。

1 个答案:

答案 0 :(得分:1)

尝试将group_wine对象复数为group_wines

class TastingGroup < ActiveRecord::Base
  has_many :group_wines
  has_many :wines, through: :group_wines
end

class GroupWine < ActiveRecord::Base
  belongs_to :tasting_group
  belongs_to :wine
end

class Wine < ActiveRecord::Base      
  has_many :group_wines
  has_many :tasting_groups, through: :group_wines
end