comments = Song.find(65).comments.select('comments.*, users.*').includes(:user).limit(20)
如何將用戶包含在結果集中?如何將用戶包含在結果集中?
進出口使用的PusherApp,並嘗試做:
Pusher["foo"].trigger("bar", {
:comments => comments
})
comments = Song.find(65).comments.select('comments.*, users.*').includes(:user).limit(20)
如何將用戶包含在結果集中?如何將用戶包含在結果集中?
進出口使用的PusherApp,並嘗試做:
Pusher["foo"].trigger("bar", {
:comments => comments
})
如果您想用自定義字段快速生成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)
,與oj gem。
Rails不再使用'type_cast_attribute' - 你會怎麼做? –
取代包括(:用戶)與連接(:用戶),您將能夠選擇用戶字段
comments = Song.find(65).comments.select('comments.*, users.*').joins(:user).limit(20)
只返回評論對象 –
請解釋你的意思是包括用戶。如果您選擇包括用戶在內的評論,他們將被加載到'comment.user'中 – Serabe
'comment.user'將不會在Pusher json對象中被訪問 –
'Song.find(65).comments.includes(:user) .limit(20).map {| comment | {:comment => comment,:user => comment.user}}'就像這樣。但尋找更有效的方法 –