I have a few scenarios where I would like to do as little DB calls as much as possible via eager loading, but I have not been able to do it well.
Given the 2 scenarios below, how can i change my RABL to do as little calls as possible?
OBJECT MODEL:
Posts
-> belongs_to user
-> has_many: Comments
-> Comment belongs_to user
-> has_many: Tags
-> Tag belongs_to user
RABL (Both of these will cause the DB to do many individual calls)
node(:comments) do |p|
p.filtered_comments(@user)
end
child :tags do
attribute :text
child :users do
attribute :nickname
end
end
CONTROLLER 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