rabl icon indicating copy to clipboard operation
rabl copied to clipboard

Eager loading not working when calling Rabl::Renderer directly

Open funtusov opened this issue 10 years ago • 17 comments

Rails 4, RABL 0.9.4pre1 and 0.9.3, Mongoid 4.0.0.beta1

In one controller I have an action like

  def index
    @bookmarks = current_user.bookmarks.includes(:channels, :user)
  end

Which renders the rabl template that includes references to model's relations hitting the db only a couple of times.

But when I try

@bookmarks = current_user.bookmarks.includes(:channels, :user)
Rabl::Renderer.json(@bookmarks, 'api/bookmarks/index')

This gets the N+1 query problem. The template is the same.

I tried:

  • to pass in the controller scope Rabl::Renderer.json(@bookmarks, 'api/bookmarks/index', scope: self)
  • to put .includes inside the call Rabl::Renderer.json(@bookmarks.includes(:channels, :user), 'api/bookmarks/index')
  • Using different styles of initializing Rabl::Renderer

This is the code that works correctly, as I understand:

  module ActionView
    module Template::Handlers
      class Rabl
        class_attribute :default_format
        self.default_format = Mime::JSON

        def self.call(template)
          source = template.source

          %{ ::Rabl::Engine.new(#{source.inspect}).
              render(self, assigns.merge(local_assigns)) }
        end # call
      end # rabl class
    end # handlers

Any ideas of why it may be happening?

funtusov avatar May 16 '14 20:05 funtusov

+1, running into this issue right now myself. @funtusov have you managed to find any solutions?

DeepAnchor avatar May 31 '14 06:05 DeepAnchor

+1, been unable to eager load anything using Rabl

finch1642 avatar Jan 05 '15 18:01 finch1642

+1 Can't use Rabl in production with this bug.

daveroberts avatar Jan 05 '15 18:01 daveroberts

If anyone can track down the culprit and submit a pull request, I'd love to merge. Are you all using Mongoid when you see this issue?

nesquena avatar Jan 12 '15 19:01 nesquena

@finch1642 and @daveroberts; are you seeing the same issue @funtusov is, where eager loading works with regular controller/action/view, just not when using Rabl::Renderer directly, or are you seeing eager loading never work at all?

DouweM avatar Jan 19 '15 16:01 DouweM

@DouweM I am seeing eager loading never work at all

finch1642 avatar Jan 19 '15 16:01 finch1642

What versions of Rabl and ActiveRecord/Mongoid are you on and what do your controller and views look like?

DouweM avatar Jan 19 '15 16:01 DouweM

ActiveRecord 4.1.7 Rabl 0.11.4 Using multiple SQL databases and experiencing the same issue

My controllers/views vary in complexity, but even the most simple scenario doesn't utilize eager loading.

Controller:

@posts = Post.all.includes(:comments)
render "posts/index.json.rabl"

Views:

posts/index.json.rabl

collection @posts 
attributes :id 

child :comments do
 extends "comments/index"
end

comments/index.json.rabl

collection @comments
attributes :id,:text

finch1642 avatar Jan 19 '15 16:01 finch1642

Could you try again with Rabl master and let me know how that goes? I have no idea if the recent changes affect it in any way, but that way we're at least on the same page.

DouweM avatar Jan 19 '15 17:01 DouweM

Still not working, at least with mongoid.

krzysztofsciubisz avatar Feb 04 '15 15:02 krzysztofsciubisz

+1 it does not work with mongoid.

You can nevertheless use:

@posts = Post.all.includes(:comments).cache
respond_with @posts

In that case the database won't be hit again during the view rendering

abrisse avatar Mar 11 '15 11:03 abrisse

+1 any solution/workaround?

nitzanav avatar Aug 05 '15 13:08 nitzanav

+1

jkowens avatar Nov 10 '15 18:11 jkowens

+1

kerim1510 avatar May 17 '16 13:05 kerim1510

+1

taylor-a-barnette avatar Nov 07 '17 00:11 taylor-a-barnette

+1

oaguy1 avatar Mar 04 '18 12:03 oaguy1

+1

Mitch1000 avatar Oct 02 '18 21:10 Mitch1000