Search code examples
ruby-on-railsgrailsgrails-ormarel

Grails equivelant of Rails ARel 'includes'


I have a Rails app that I was able to speed up significantly using ARel "includes" e.g. (contrived)

class User < ActiveRecord::Base
   has_many :posts
   scope :eager, includes(:posts => [:rating, :author, {:tags => [:day, {:foo => :bar}]}] )
end

Calling

@posts = current_user.posts.eager

reduces that page load hugely and reduces the number queries by a very large factor. Rails does this by first selecting the posts in one query

select * from posts where ...

and then selecting all the comments for all those posts in one query instead of one query per comment:

select * from comments where post_id in (6,7,8,9,10,...)

Is there an equivalent in grails? I am familiar with criteria and named queries where I could write a query with a lot of joins but what I want is for Grails to produce a few queries with "IN" operator.


Solution

  • I found some references to this problem: Eager and Lazy Fetching and fetchMode.