title: Rails 'DB - no such column' error for has_many through relationship
body: <p>I created a <code>has_many :through</code> relationship for a rails app I am creating. However, I am unable have nested attributes for the many to many relationship.</p>
  
  <p>My models are as follows:</p>
  
  <p><strong>article.rb</strong></p>
  
  <pre><code>class Article &lt; ActiveRecord::Base
    attr_accessible :body, :title
    has_many :article_tags
    has_many :tags, :through =&gt; :article_tags, :foreign_key =&gt; :tag_id    
    accepts_nested_attributes_for :tags, reject_if: :all_blank    
    attr_accessible :tags_attributes    
    validates_presence_of [:body, :title]
  end
  </code></pre>
  
  <p><strong>tag.rb:</strong></p>
  
  <pre><code>class Tag &lt; ActiveRecord::Base
     has_many :article_tags
     has_many :articles, :through =&gt; :article_tags, :foreign_key =&gt; :article_id
     attr_accessible :name
     validates_presence_of :name
  
     accepts_nested_attributes_for :articles, reject_if: :all_blank
  
     attr_accessible :articles_attributes
  end
  </code></pre>
  
  <p><strong>article_tag.rb:</strong></p>
  
  <pre><code>class ArticleTag &lt; ActiveRecord::Base
   belongs_to :article
   belongs_to :tag
  end
  </code></pre>
  
  <p>Now in my view for the article (show.html.slim), I want to show all tags that belong to the article using the line:</p>
  
  <pre><code>- @article.tags.each do |t|
    = t.name
  </code></pre>
  
  <p>However, I get the error:
  <code>SQLite3::SQLException: no such column: article_tags.article_id: SELECT "tags".* FROM "tags" INNER JOIN "article_tags" ON "tags"."id" = "article_tags"."tag_id" WHERE "article_tags"."article_id" = 1</code></p>
  
  <p>This problem is solved.</p>
  
  <p>For anybody who needs to know:</p>
  
  <p>You must <strong>manually</strong> add the foreign keys in the migration and the schema. I added them and voila! it works!</p>
  
  <p>Thanks to Dan Reedy for his help!</p>