2013-02-18 122 views
0

我遇到了通過記錄更新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子句?

謝謝。

回答

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 
+0

是的,這個工作,而且我需要對錶中的主ID。它曾經是我改變的HABTM,但忘了添加主鍵。謝謝! – Paul 2013-02-18 22:44:50