{"id":1232,"date":"2022-08-30T15:14:35","date_gmt":"2022-08-30T15:14:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/activerecord-unknown-column-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:35","modified_gmt":"2022-08-30T15:14:35","slug":"activerecord-unknown-column-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/activerecord-unknown-column-collection-of-common-programming-errors\/","title":{"rendered":"ActiveRecord unknown column-Collection of common programming errors"},"content":{"rendered":"<p>I ran into a little problem I have a has_many through relationship here is the code for the models<\/p>\n<pre><code>class User &lt; ActiveRecord::Base\n  has_many :friendships\n  has_many :followings, :through =&gt; :friendships, :foreign_key =&gt; \"followed_id\"\nend\n\nclass Friendship &lt; ActiveRecord::Base\n  belongs_to :user \n  belongs_to :following, :class_name =&gt; \"User\", :foreign_key =&gt; \"followed_id\"\nend\n<\/code><\/pre>\n<p>now at the console I can type u = User.first and then u.friendships.first.following this gives me the first user u is following, but when I type u.friendships.last.following I get this error<\/p>\n<p>the SELECT statement from u.friendships.first.following<\/p>\n<pre><code>Friendship Load (0.3ms)  SELECT `friendships`.* FROM `friendships` WHERE `friendships`.`user_id` = 208 LIMIT 1\nUser Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 209 LIMIT 1\n<\/code><\/pre>\n<p>and the SELECT statement from u.friendships.last.following<\/p>\n<pre><code>Friendship Load (0.3ms)  SELECT `friendships`.* FROM `friendships` WHERE `friendships`.`user_id` = 208 ORDER BY `friendships`.`` DESC LIMIT 1\n<\/code><\/pre>\n<pre><code>ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'friendships.' in 'order\nclause': SELECT  `friendships`.* FROM `friendships`  WHERE `friendships`.`user_id` = 208\nORDER BY `friendships`.`` DESC LIMIT 1\n<\/code><\/pre>\n<p>if I then run u.friendships and then u.friendships.last.following again, I don&#8217;t get the error anymore, why is that?<\/p>\n<ol>\n<li>\n<p>Heres my sql output for <code>friendships<\/code>, straight from your code on <strong>Rails 3.2.9 \/ postgresql<\/strong>:<\/p>\n<pre><code># u.friendships.first.following\nFriendship Load (0.9ms)  SELECT \"friendships\".* FROM \"friendships\" WHERE \"friendships\".\"user_id\" = 1 LIMIT 1\n\n# u.friendships.first.following\nFriendship Load (1.3ms)  SELECT \"friendships\".* FROM \"friendships\" WHERE \"friendships\".\"user_id\" = 1 ORDER BY \"friendships\".\"id\" DESC LIMIT 1\n<\/code><\/pre>\n<p>So for some reason for me, <code>id<\/code> is getting picked up automatically in <code>ORDER BY \"friendships\".\"id\"<\/code> and it works. Maybe your problem has something to do with your DB?<\/p>\n<pre><code>#Statements used to create the db for reproducing this problem\nCREATE TABLE users (id SERIAL PRIMARY KEY)\nCREATE TABLE friendships (\n    id            SERIAL PRIMARY KEY,\n    user_id       integer\n    followed_id   integer \n);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:11:12. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I ran into a little problem I have a has_many through relationship here is the code for the models class User &lt; ActiveRecord::Base has_many :friendships has_many :followings, :through =&gt; :friendships, :foreign_key =&gt; &#8220;followed_id&#8221; end class Friendship &lt; ActiveRecord::Base belongs_to :user belongs_to :following, :class_name =&gt; &#8220;User&#8221;, :foreign_key =&gt; &#8220;followed_id&#8221; end now at the console I can [&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-1232","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1232","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=1232"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1232\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}