Querying multiple linked classes in Rails

I want to query multiple tables. For example in posts table there is a user_id linked to users id. While showing every post, I also want to display the picture of the user. My approach is this, but there is a problem. @user.picture method is undefined.

<% @programs.each do |post| %>
<%= @user = User.where("id = post.user_id") %>
<li class="one-third column">                         
<div class="wrapper">
<div class="postThumb"><img src="<%= @user.picture %>" /></div>
  <div class="postDetails">
    <%= link_to "#{ post.title.upcase! }".html_safe, all_posts_path, :class => "postTitle" %>
    <p><%= truncate post.details, :length => 90 %></p>
  </div> 
</div>
</li>
<% end %>

Program Controller:

class ProgramController < ApplicationController
def index
  @programs = Program.all
end

User model:

class User < ActiveRecord::Base

  attr_accessible :password, :username, :oauth_token, :provider, :uid, :oauth_expires_at, :picture, :email, :name, :location, :gender, :updated_at, :is_admin
  has_many :posts   
  has_one :program
  has_many :programdetails, :through => :program
end

Program model:

class Program < ActiveRecord::Base
  attr_accessible :details, :title, :user_id
  belongs_to :user
  has_many :programdetails
end

Answers


Try this instead, in the controller:

@programs = Program.includes(:user) # this will return all programs and related users in
                                    # 2 database queries rather than N+1 queries

Then in the view:

<div class="postThumb"><img src="<%= post.user.picture %>" /></div>

Also, you can use image_tag instead.

Finally, you can probably change your post title link to:

<%= link_to post.title.upcase, all_posts_path, :class => "postTitle" %>

@user = post.user. Rails association will give back the associated user by itself.

And to correct above, syntactically, @user = User.find(post.user_id)


try changing this: @user = User.where("id = post.user_id")

into this: @user = User.where(id: post.user_id).first

or even better: @user = post.user as suggested by kiddorails


There is a much easier way to do this using the relations you have already defined:

Programs Controller
def index
  @programs = Program.
    includes(:user). # eager load user relation to avoid n+1
    all
end
View
<% @programs.each do |post| %>
  <li class="one-third column">                         
    <div class="wrapper">
      <div class="postThumb"><img src="<%= post.user.picture %>" /></div>
        <div class="postDetails">
          <%= link_to "#{ post.title.upcase! }".html_safe, all_posts_path, :class => "postTitle" %>
          <p><%= truncate post.details, :length => 90 %></p>
      </div> 
    </div>
  </li>
<% end %>

Need Your Help

Plot a CDF chart by Microsoft Excel

excel math charts plot

I'm not quite sure ask this question here or on SuperUser,

Showing HTML block on click of the preview image?

php jquery html

I want to show the HTML block on the click of Preview image.For this i used jquery image gallery.which works like to click on a image and show the full image but i want to show the HTML block inste...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.