Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does Garner support binding ActiveRecord models? #84

Open
mfunaro opened this issue Dec 10, 2014 · 7 comments
Open

Does Garner support binding ActiveRecord models? #84

mfunaro opened this issue Dec 10, 2014 · 7 comments

Comments

@mfunaro
Copy link

mfunaro commented Dec 10, 2014

Just getting started with Garner and I am having trouble binding to ActiveRecord models. I debugged down into the code and it seems that if you bind an ActiveRecord model class such as garner.bind(Patient) ... end, by default the SafeCacheKey strategy will be used, but since the model class wont respond to :cache_key or :updated_at, we return from the apply method of the strategy with no binding_keys which effectively skips the cache.

Am I doing something wrong? I tried figure out what to do with ActiveRecord from the Mongoid samples.

Any help would be greatly appreciated, also sorry if this is not the correct place for questions, I looked for a Google Group and didn't find one.

@mfunaro
Copy link
Author

mfunaro commented Dec 11, 2014

After some more spelunking through the code, I see that my use case is not implemented yet. No need to look into this for me. I'm going to see if I can replicate some of the Mongoid features in ActiveRecord.

@dblock
Copy link
Contributor

dblock commented Dec 11, 2014

Would be really happy to see complete ActiveRecord support! Currently we say:

Garner provides rudimentary support for ActiveRecord. To use ActiveRecord models for Garner bindings, use Garner::Mixins::ActiveRecord::Base. You can set it up in an initializer:

require "garner/mixins/active_record"

module ActiveRecord
  class Base
    include Garner::Mixins::ActiveRecord::Base
  end
end

@fancyremarker
Copy link
Contributor

Hey @mfunaro, I'm using Garner w/ ActiveRecord, but can confirm that binding to classes won't work out of the box (just instances). In order to bind to classes, the ActiveRecord mixin would need to implement ActiveRecord::Base.proxy_binding as the Mongoid mixin does. For Mongoid, this looks like:

# Only find the latest if we can order by :updated_at
return nil unless fields['updated_at']
only(:_id, :_type, :updated_at).order_by(updated_at: :desc).first

It'd be similar for ActiveRecord. Something like this would work (though it's not optimized):

return nil unless columns.map(&:name).include?('updated_at')
order('updated_at DESC').first

@mfunaro
Copy link
Author

mfunaro commented Dec 12, 2014

Thanks for the responses. @fancyremarker, I did end up doing something very similar to what you suggested to get things working in a fork. I also created an identity for ActiveRecord so that I could use Patient.identify(params[:id])

@dblock
Copy link
Contributor

dblock commented Dec 12, 2014

Looking forward to some pull requests!

@danielkummer
Copy link

👍 on this...

@ericgross
Copy link

Would love to see a PR for this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants