Q:

Rails: How do you use a search query to find a belongs_to item as well?

What I need is to search for Story titles as well as who the story belongs to all in one search.
Obviously this isn't right but it gets my point across:
@stories = Story.where("user.display_name LIKE ? And title LIKE ?", 
             "%#{@query}%", "%#{@query}%").limit(500).page(params[:page])

Stories belong to users and users have many stories.  Not sure what else I need to put here and I haven't really understood any answers that seem to be related.
Thanks.
UPDATE
<% if @stories.size == 0 %>
    Sorry, there were no stories found.
<% else %>
    <table class="sortableTable table-striped" style="width: 100%; text-align: left;">
      <thead>
        <tr>
         <th width="55%">Title</th>
         <th width="25%">User</th>
         <th width="10%">Created At</th>
         <th width="10%">Updated At</th>
        </tr>
      </thead>
      <tbody>
        <% @stories.each do |s| %>
        <tr>
         <td width="55%"><div class="span5 search_result"><%= link_to s.title, s %></div></td>
         <td width="25%"><div class="span3 search_result"><%= link_to s.user.display_name, s.user %></div></td>
         <td width="10%"><div class="span3 search_result"><%= time_ago_in_words(s.created_at) %> ago</div></td>
         <td width="10%"><div class="span3 search_result"><%= time_ago_in_words(s.updated_at) %> ago</div></td>
        </tr>
        <% end %>

      </tbody>
    </table>
<% end %>

A:

If you don't want to eager load the user object associated with the story:
Story.joins(:user).where(
   "users.display_name LIKE :term And stories.title LIKE :term", 
   :term =>  "%#{@query}%"
).limit(500).page(params[:page])

If you want to eager load the user object associated with the story:
Story.includes(:user).where(
   "users.display_name LIKE :term And stories.title LIKE :term", 
   :term =>  "%#{@query}%"
).limit(500).page(params[:page])

Note:
I am using the placeholder condition in the where clause as the same query term is used in two places.

