Rails 3从Join中返回所有列

Dex*_*Dex 6 join ruby-on-rails arel

我正在尝试连接两个表并返回所有列,而不仅仅是与模型相关的列.

我有类似的东西:

Comment.joins(:user).select("*")
Run Code Online (Sandbox Code Playgroud)

SQL看起来很好,但是,它只返回注释而不返回与之关联的用户信息.

我怎样才能检索*而不仅仅是comments.*

rwi*_*ams 8

关于什么

comments = Comment.includes(:user).all
Run Code Online (Sandbox Code Playgroud)

现在comments将是一个数组,所以你必须循环遍历它才能看到所有用户.

#won't work
comments.user 

#should work
comments[0].user

comments.each do |comment|
    puts comment.user.name #or whatever
end
Run Code Online (Sandbox Code Playgroud)