问题更新has_many:通过记录
问题描述:
我遇到了通过记录更新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模型。
但是,当我尝试更新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
什么用NULL为group_wines升起来了,为什么它补充说,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
是的,这个工作,而且我需要对表中的主ID。它曾经是我改变的HABTM,但忘了添加主键。谢谢! – Paul 2013-02-18 22:44:50