Ok.. I'm new to Rails, and I know this has been asked before, but I'm still confused as to how to approach the following common problem. I can get the association to work, but having something magically work and starting rails with bad habits is not something I want to do.
Say I'm building a blog. I have two resources: Articles and Users. Each user has many articles, and each article belongs to one user:
rails g scaffold User name:string email:string
rails g scaffold Article user_id:integer title:string content:string
User Model:
class User < ActiveRecord::Base
has_many :articles
end
Article Model:
class Article < ActiveRecord::Base
belongs_to :user
end
Now, on my articles index, I can do something like:
…table headers...
<% @articles.each do |article| %>
<tr>
<td><%= article.user.name %></td>
<td><%= article.title %></td>
<td><%= article.desc %></td>
<td><%= article.content %></td>
<td><%= link_to 'Show', article %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
And all I need for that model association for the User Name is to put "@articles = Article.all" on the index action before the respond_to. Pretty cool!
What if I want to list all these articles (I'm skipping paging here for the sake of simplicity) on my Home page, using the index action on my Home controller?
I know I can do something like this in the Home Controller:
class HomeController < ApplicationController
def index
@articles = Article.joins(:user)
end
end
…and then I can access this data on my home->index view:
<div class="row">
<% @articles.each do |article| %>
<div>
<h3><%= link_to article.title,
:controller => "articles", :action => "show", :id => article.id %></h3>
<small>Posted on <%= article.created_at %> by
<a href="#"><%= article.user.name %></a></small>
</div>
<% end %>
</div>
First Question: When accessing the User data for all the Articles, should I be using a :joins or an :includes? It seems they both work, but I wonder which one is right in this situation, and which one generally performs faster.
@articles = Article.joins(:user)
-vs-
@articles = Article.includes(:user)
Second Question: In my scaffold for Article (building the migration), should I use user_id:integer or user:references. Do they do the same thing, or is one preferred over the other? If I use :integer as the field type, is it recommended that I also add an index for it (add_index :articles, :user_id)? I found a great RailsCast, and it does a great job of explaining, but I wonder if anyone else has another opinion.
If it helps, I'm on Rails 3.2.2.
Thanks!