retire icon indicating copy to clipboard operation
retire copied to clipboard

Proposal / potential PR: Support for custom load parameters

Open fgrehm opened this issue 12 years ago • 5 comments

Hey @karmi,

I've just started experimenting with the code below and I was wondering if it is something that could be added to Tire's core or to tire-contrib:

Tire::Results::Collection.class_eval do
  def __find_records_by_ids(klass, ids)
    if @options[:load].is_a?(Hash)
      klass.find(ids, @options[:load][klass])
    else
      @options[:load] === true ? klass.find(ids) : klass.find(ids, @options[:load])
    end
  end
end

The idea would be to support fine grained eager loading on multi model searches. If you think it is a nice idea, I'd be more than happy to turn this issue into a proper pull request either here or on tire-contrib :)

fgrehm avatar Jun 19 '13 20:06 fgrehm

@fgrehm Sounds like a nice idea, but could you please add a more fleshed out example?

karmi avatar Jun 21 '13 11:06 karmi

@karmi sure, I'll get back to u on monday :)

fgrehm avatar Jun 21 '13 21:06 fgrehm

@karmi here's a simple gist with example models and usage, this is basically what I'm currently doing on the app I'm working on. I hope they are self explanatory but if u need more info just LMK :) our current need is to perform a global / multi model search and render associations data on the search results page alongside each result item so we ended up doing that to avoid N+1 queries

fgrehm avatar Jun 26 '13 05:06 fgrehm

Will have a look. At the moment, the biggest problem is related to #767, and with the current release of Rails 4, the problem is more imminent.

karmi avatar Jun 26 '13 07:06 karmi

@karmi got it, but the issues are not mutually exclusive as there's currently no way to specify custom eager loading behavior per class on multi model searches :) I've updated the gist adding an example of how it could be used with lambdas

fgrehm avatar Jun 26 '13 14:06 fgrehm