如何在结果集中包含用户?

时间:2012-11-07 10:40:04

标签: ruby-on-rails ruby ruby-on-rails-3 pusher

comments = Song.find(65).comments.select('comments.*, users.*').includes(:user).limit(20) 

如何在结果集中包含用户?

我正在使用PusherApp并尝试执行:

    Pusher["foo"].trigger("bar", { 
      :comments => comments
    })

2 个答案:

答案 0 :(得分:1)

如果您想使用自定义字段快速生成json,最好阅读this tutorial

它基本上使用这样的方法:

def self.lightning
  connection.select_all(select([:latitude, :longitude, :timestamp, :virtual_odometer]).arel).each do |attrs|
    attrs.each_key do |attr|
      attrs[attr] = type_cast_attribute(attr, attrs)
    end
  end
end

返回一个哈希数组。

然后使用render json: Oj.dump(@statuses.lightning, mode: :compat)使用{{1}}。

答案 1 :(得分:0)

将include(:user)替换为连接(:user),您将能够选择用户字段

comments = Song.find(65).comments.select('comments.*, users.*').joins(:user).limit(20)