When to use association extensions vs named scopes?

耗尽温柔 提交于 2019-12-19 06:47:46

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/2187357/when-to-use-association-extensions-vs-named-scopes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!