开发者

named scope vs module in ruby on rails

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜