When to use association extensions vs named scopes?

前端 未结 2 1189
情书的邮戳
情书的邮戳 2021-01-12 20:27

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 ar

相关标签:
2条回答
  • 2021-01-12 21:04

    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

    0 讨论(0)
  • 2021-01-12 21:07

    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.

    0 讨论(0)
提交回复
热议问题