我想查询多个表。例如,在帖子表中,有一个 user_id 链接到用户 ID。在显示每个帖子的同时,我还想显示用户的图片。我的方法是这样的,但是有一个问题。@user.picture
方法未定义。
<% @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 %>
程序控制器:
class ProgramController < ApplicationController
def index
@programs = Program.all
end
用户型号:
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
程序模型:
class Program < ActiveRecord::Base
attr_accessible :details, :title, :user_id
belongs_to :user
has_many :programdetails
end