0

我在通过记录更新 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 模型中添加了一个“位置”属性。

但是,当我尝试更新 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 子句?

谢谢。

4

1 回答 1

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
于 2013-02-18T22:35:54.553 回答