我的索引控制器:
class IndexController < ApplicationController
def index
@posts = Post.find(:all, :limit => 10,
:joins => "LEFT JOIN `users` ON users.user_id = posts.user_id",
:select => "posts.*, users.username",
:order => "posts.created_at DESC")
end
end
如何从视图中访问 users.username?
我试过
<% @posts.each do |post| %>
<%= post.username %>
<% end %>
但它似乎不起作用,因为我收到一条空白消息......
编辑:
我不得不在 LEFT JOIN 中使用以下代码:
:joins => "LEFT JOIN `users` ON posts.user_id = users.id",