Logan Koester
Logan Koester

Reputation: 1146

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?

Upvotes: 7

Views: 2920

Answers (2)

Ben Crouse
Ben Crouse

Reputation: 8348

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

Upvotes: 12

westoque
westoque

Reputation: 417

Yes, you are right, the main difference is that named_scope's are chainable. But they basically do the same function, its just that association extensions are faster.

Upvotes: 0

Related Questions