{"id":1244,"date":"2022-08-30T15:14:47","date_gmt":"2022-08-30T15:14:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/activerecord-has_many-and-custom-foreign_key-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:47","modified_gmt":"2022-08-30T15:14:47","slug":"activerecord-has_many-and-custom-foreign_key-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/activerecord-has_many-and-custom-foreign_key-collection-of-common-programming-errors\/","title":{"rendered":"ActiveRecord :has_many and custom foreign_key-Collection of common programming errors"},"content":{"rendered":"<p>I have the following structure:<\/p>\n<pre><code>class Product &lt; ActiveRecord::Base\n    has_many :product_units\n    has_many :units, :through =&gt; :product_units, :foreign_key =&gt; :unitID\nend\n\nclass Unit &lt; ActiveRecord::Base\n    has_many :product_units\n    has_many :products, :through =&gt; :product_units, :foreign_key =&gt; :productID\nend\n\nclass ProductUnit &lt; ActiveRecord::Base\n    belongs_to :unit, :foreign_key =&gt; :unitID\n    belongs_to :product, :foreign_key =&gt; :productID\nend\n<\/code><\/pre>\n<p>but as I try to run<\/p>\n<pre><code>Product.last.units.create({:name =&gt; 'test', :symbol =&gt; 't'})\n<\/code><\/pre>\n<p>The following error occurs :<\/p>\n<pre><code>ActiveRecord::UnknownAttributeError: unknown attribute: product_id\n<\/code><\/pre>\n<p>Can someone help me ? it&#8217;s driving me crazy \ud83d\ude21<\/p>\n<p><strong>EDIT:<\/strong> When I try the following:<\/p>\n<pre><code>Product.last.units\n<\/code><\/pre>\n<p>I get the error<\/p>\n<pre><code>SELECT \"units\".* FROM \"units\" INNER JOIN \"product_units\" ON \"units\".\"unitID\" = \"product_units\".\"unitID\" WHERE \"product_units\".\"product_id\" = 1\n<\/code><\/pre>\n<p>The problem is that product_units has no product_id field, instead its name is productID; why the identifier name is correct on join but not in the where statement ?<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:51. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have the following structure: class Product &lt; ActiveRecord::Base has_many :product_units has_many :units, :through =&gt; :product_units, :foreign_key =&gt; :unitID end class Unit &lt; ActiveRecord::Base has_many :product_units has_many :products, :through =&gt; :product_units, :foreign_key =&gt; :productID end class ProductUnit &lt; ActiveRecord::Base belongs_to :unit, :foreign_key =&gt; :unitID belongs_to :product, :foreign_key =&gt; :productID end but as I try to [&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-1244","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1244","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=1244"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1244\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}