Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use association extensions vs named scopes?

From a cursory glance, they appear to be simply two different approaches to the same set of problems, except that named scopes are chainable, while association extensions are not.

Can anyone explain further, or provide an example that would be more appropriate for an association extension than a named scope?

like image 1000
Logan Koester Avatar asked Feb 02 '10 20:02

Logan Koester


1 Answers

Association extensions are very useful for creating custom methods for creating, updating, etc (not necessarily finding).

Because you have access to the proxy_owner, proxy_reflection, proxy_target, you have a nice hook into the relationship.

Check out the Association Extension section of the Rails docs:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

like image 98
Ben Crouse Avatar answered Nov 05 '22 21:11

Ben Crouse