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

Rails Scopes don't invoke Find method override #9

Open
ryanchuleff opened this issue May 22, 2011 · 2 comments
Open

Rails Scopes don't invoke Find method override #9

ryanchuleff opened this issue May 22, 2011 · 2 comments

Comments

@ryanchuleff
Copy link

The issue is basically that if I do a Parent.find(my_id) it will return the fully parsed object with parent and child attributes included, but if I declare a scope called foo on the Parent and define it as "where("type= ? and vendor_id = ?", @widget.type, @vendor.id)" and then call Parent.foo, it returns only the parents attributes.

APIDock acknowledges as much here: http://api.rubyonrails.org/classes/ActiveRecord/NamedScope/ClassMethods.html

It doesn't seem to say though what it is using instead so its hard to say where it needs to be overridden. I have a sinking suspicion that because each declared scope is instantiated as a method on the ActiveRecord class, you would have to override them individually, or at least force a call out of some sort to handle the override in a generic fashion.

Anyway, there is a workaround by doing Parent.find(:all, :conditions => "(type=#{@widget.type} and vendor_id=#{@vendor.id})") which seems to work just find. It just forces you to define the logic in each controller instead of obfuscating in the model.

Let me know if you have any ideas.

-Ryan

@petehamilton
Copy link
Owner

I have a similar feeling that it may be necessary to override methods in ActiveRecord for functions utilising the database which is a bit of a nightmare.
So far I have been doing simple save/update functionality and so obviously it hasn't been an issue, perhaps there is some way in rails to intercept a scoped method or create something similar to the "before_save" function but for generic scopes?

@DouweM
Copy link

DouweM commented Sep 4, 2011

Fixed in pull request #31's commits 51aa7dc + 6ddc33d!

petehamilton pushed a commit that referenced this issue Oct 20, 2011
Fixed issues #9, #13, #26, #27, #28, #29, #30, and some more.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants