title: Rails Associations: access join table by console (HABTM)
body: <p>I got what I wanted - has and belongs to many associations, using the simple directives from the Rails Guides. In the console it all works just fine, but I am kind of stuck with the following.</p>
  
  <p>Let's say I have two models <strong>Article</strong> and <strong>Category</strong>, which both have the directive has_and_belongs_to_many in their models and corresponding to the articles_categories table in the database. In rails console I can see the association working with statements such as these:</p>
  
  <pre><code>%&gt;   @x = Article.find(1)
  %&gt;   @x.categories
  </code></pre>
  
  <p>This way I have a collection of categories stored inside @x. Wonderful. But I cannot really find a way how I could 'add' through console a new category. Right now, I am using SQL to insert the values into the jointable. I am hoping there is a much smarter, Railsy way to do something like this</p>
  
  <pre><code>%&gt; @x.article.categories.category_id = 1  # id of category
  %&gt; @x.article.categories.article_id = 1  # id of article
  %&gt; @x.save # and written to the database
  </code></pre>
  
  <p>I am specifically looking for the way to do this in the rails console - so I actually get the feeling of what is happening instead of code snippets that work and I don't get. I am using Rails <strong>4.1.6</strong></p>