{"id":1013,"date":"2022-08-30T15:10:56","date_gmt":"2022-08-30T15:10:56","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/trouble-update-has_many-through-record-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:56","modified_gmt":"2022-08-30T15:10:56","slug":"trouble-update-has_many-through-record-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/trouble-update-has_many-through-record-collection-of-common-programming-errors\/","title":{"rendered":"Trouble update has_many :through record-Collection of common programming errors"},"content":{"rendered":"<p>I am having an issue updating a has_many through record. Here is my setup:<\/p>\n<pre><code>class TastingGroup &lt; ActiveRecord::Base\n  has_many :group_wine\n  has_many :wines, through: :group_wine\nend\n\nclass GroupWine &lt; ActiveRecord::Base\n  belongs_to :tasting_group\n  belongs_to :wine\nend\n\nclass Wine &lt; ActiveRecord::Base      \n  has_many :group_wine\n  has_many :tasting_groups, through: :group_wine\nend\n<\/code><\/pre>\n<p>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 &#8216;position&#8217; attribute to the GroupWine model.<\/p>\n<p>However, when I try to even update a GroupWine record, I get the following error, and here is what I am doing.<\/p>\n<pre><code>gw = GroupWine.first\n# {\n         :wine_id =&gt; 1,\n         :tasting_group_id =&gt; 1,\n         :position =&gt; nil\n}\n\ngw.position = 1\ngw.save\n<\/code><\/pre>\n<p>And here is the error I get&#8230;<\/p>\n<pre><code>ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'group_wines.' in 'where clause': UPDATE `group_wines` SET `position` = 3 WHERE `group_wines`.`` IS NULL\n<\/code><\/pre>\n<p>What is up with the NULL for group_wines, and why is it adding that where clause?<\/p>\n<p>Thanks.<\/p>\n<ol>\n<li>\n<p>Try pluralizing the <code>group_wine<\/code> object to <code>group_wines<\/code><\/p>\n<pre><code>class TastingGroup &lt; ActiveRecord::Base\n  has_many :group_wines\n  has_many :wines, through: :group_wines\nend\n\nclass GroupWine &lt; ActiveRecord::Base\n  belongs_to :tasting_group\n  belongs_to :wine\nend\n\nclass Wine &lt; ActiveRecord::Base      \n  has_many :group_wines\n  has_many :tasting_groups, through: :group_wines\nend\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:13:42. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am having an issue updating a has_many through record. Here is my setup: class TastingGroup &lt; ActiveRecord::Base has_many :group_wine has_many :wines, through: :group_wine end class GroupWine &lt; ActiveRecord::Base belongs_to :tasting_group belongs_to :wine end class Wine &lt; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1013","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1013"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1013\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}