2
我有一些場景,我希望儘可能少的數據庫通過預先加載進行調用,但是我一直無法做到。在使用RABL和mongoid時減少數據庫調用
鑑於以下兩種情況,我如何改變我的RABL以儘可能少地打電話?
對象模型:
Posts
-> belongs_to user
-> has_many: Comments
-> Comment belongs_to user
-> has_many: Tags
-> Tag belongs_to user
Rabl的(這些將導致DB做許多個人電話)
node(:comments) do |p|
p.filtered_comments(@user)
end
child :tags do
attribute :text
child :users do
attribute :nickname
end
end
控制器QUERY
Post.includes(user, comments, tags)...
POST。 RB
def filtered_comments
comments = self.comments.where(:blocked=>false).all
json = Rabl::Renderer.json(comments, 'comments/list', view_path: 'app/views')
JSON.parse(json).map do |c|
c['comment']
end
end
你有多少個查詢正在發生的日誌? –
如果用戶不接受任何內容,如何將用戶傳遞給'filtered_comments'方法? –