ruby on rails-故障更新has_many:通过记录



我在更新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子句?

谢谢。

尝试将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

最新更新