Trouble update has_many :through record-Collection of common programming errors

I am having an issue updating a has_many through record. Here is my setup:

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

I was trying to use the acts_as_list for this, because the order of the wines in a TastinGroup matter, so I have added a ‘position’ attribute to the GroupWine model.

However, when I try to even update a GroupWine record, I get the following error, and here is what I am doing.

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

gw.position = 1
gw.save

And here is the error I get…

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

What is up with the NULL for group_wines, and why is it adding that where clause?

Thanks.

  1. Try pluralizing the group_wine object to 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
    

Originally posted 2013-11-09 23:13:42.