petehamilton / citier

CITIER (Class Inheritance & Table Inheritance Embeddings for Rails) is a solution for simple Multiple Class Inheritance in Rails.
88 stars 24 forks source link

Rails Scopes don't invoke Find method override #9

Open ryanchuleff opened 13 years ago

ryanchuleff commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

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