title: How to test/add data with associations in rails console
body: <p>I'm a rails beginner and I'm having a little trouble testing model associations in the Rails Console. I know this is a simple fix, however, I'm not sure that I've created the appropriate models, or generated the right migrations, so I will include this information in this question. </p>
  
  <p>I've read rails documentations (<a href="http://guides.rubyonrails.org/active_record_basics.html" rel="nofollow">http://guides.rubyonrails.org/active_record_basics.html</a> , <a href="http://api.rubyonrails.org/" rel="nofollow">http://api.rubyonrails.org/</a>), I know the answer is in there, but i'm afraid my lack of experience is preventing me from totally maximizing the available documentation. </p>
  
  <p>I need to be able to create an article that <code>has_many :categories, through: :article_categories</code> in the rails console</p>
  
  <p>Here is the model for article , article_categories, and category</p>
  
  <pre><code>class Article &lt; ActiveRecord::Base
  belongs_to :user
  has_many :article_categories
  has_many :categories, through: :article_categories
  validates :title, presence: true
  validates :content, presence: true
  validates :categories, presence: true
    end
  
  class ArticleCategory &lt; ActiveRecord::Base
  belongs_to :article
  belongs_to :category
   end
  
  class Category &lt; ActiveRecord::Base
  has_many :article_categories
  has_many :articles, through: :article_categories
  validates :names, presence: true
  
   end
  </code></pre>
  
  <p>And here are the main migrations generated</p>
  
  <pre><code>Class CreateArticles &lt; ActiveRecord::Migration
    def change
     create_table :articles do |t|
     t.string :title
     t.text :context
   end
  end
  </code></pre>
  
  <p>end</p>
  
  <pre><code>class CreateArticleCategories &lt; ActiveRecord::Migration
      def change
        create_table :article_categories do |t|
        t.belongs_to :article
        t.belongs_to :category
       t.timestamps
     end
    end
  end
  
  class CreateCategories &lt; ActiveRecord::Migration
  
      def change
        create_table :categories do |t|
         t.string :name
  
        t.timestamps
      end
    end
   end
  </code></pre>
  
  <p>So, again, the question is if all models and migrations are appropriately generated. How would I go about testing it in the rails console by adding an <code>Article</code> that has a <code>Category</code>through the <code>ArticleCategories</code> table.</p>
  
  <p>As of right new I've tested adding a new Category which works and adding an Article Category, which also works, but I do not know how to add the associations when I create an Article. </p>
  
  <p>I've used </p>
  
  <pre><code>Article.errors.full_messages
  </code></pre>
  
  <p>which clearly tells me that I need to add categories before saving the article, but again, I don't know how to do that. </p>
  
  <p>Thanks in advance!</p>