comments = Song.find(65).comments.select('comments.*, users.*').includes(:user).limit(20)
我将如何在结果集中包含用户?
im使用pusherApp并尝试做:
Pusher["foo"].trigger("bar", {
:comments => comments
})
如果要使用自定义字段快速生成JSON,则最好阅读本教程。
它基本上使用这样的方法:
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)
,与oj gem。
replace Inclast Inclast(:用户)带有Joins(:用户),您将能够选择用户字段
comments = Song.find(65).comments.select('comments.*, users.*').joins(:user).limit(20)