Pass arguments in scope

后端 未结 2 1619
再見小時候
再見小時候 2020-12-29 01:46

Can someone provide an example on how to use

scope

and parameters?

For example:

class Permission < ActiveRecord         


        
相关标签:
2条回答
  • 2020-12-29 02:03

    new syntax (ruby 1.9+), that will prevent errors even if you don't supply the user -

    scope :default_permissions_for, ->(user = nil) { ... }
    
    0 讨论(0)
  • 2020-12-29 02:13

    Use lambda scopes:

    scope :default_permissions_for, lambda{|user| { :conditions => { :user_id => user.id, :is_default => true } }
    

    Be careful because not passing a parameter to a lambda when it expects one will raise an exception.

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