开发者

named scope vs module in ruby on rails

开发者 https://www.devze.com 2023-02-27 00:05 出处:网络
Please explain with example difference between n开发者_StackOverflowamed scope and module.You would use Named scope if you want to package multiple conditions in Rails such as:

Please explain with example difference between n开发者_StackOverflowamed scope and module.


You would use Named scope if you want to package multiple conditions in Rails such as:

named_scope :active, :conditions => ['active = ?', true]
named_scope :legal_age, :conditions => ['age > ?', 21]

the good advantage of named scopes is you can chain them together. for instance:

user.active.legal_age

Module is used to group similar things together. So normally if you have code that needs to be shared across you app you would define a module in your lib directory and put classes such as.

module MyModule
   class MyClass
     ....
   end
end

Module have other use cases, this is the main one from my experience.

0

精彩评论

暂无评论...
验证码 换一张
取 消