我正在尝试联接两个表并返回所有列,而不仅仅是与模型关联的列。
我有这样的东西看起来像这样:
Comment.joins(:user).select("*")
SQL看起来不错,但仍然只返回注释,没有与之关联的用户信息。
我该如何检索
*
而不仅仅是comments.*
?
最佳答案
关于什么
comments = Comment.includes(:user).all
现在
comments
将成为一个数组,因此您必须遍历它才能查看所有用户。#won't work
comments.user
#should work
comments[0].user
comments.each do |comment|
puts comment.user.name #or whatever
end
关于ruby-on-rails - Rails 3从联接返回所有列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4292239/