複数のテーブルをクエリしたい。たとえば、投稿テーブルには、ユーザー ID にリンクされた user_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