简体   繁体   中英

IS this syntax ok for ruby 1.8.7

i have this app and while doing a rake db:migrate I get this error

      scope :my, lambda { |options = {}|
        includes(:permissions).
        where("#{quoted_table_name}.user_id     = :user OR " <<
              "#{quoted_table_name}.assigned_to = :user OR " <<
              "permissions.user_id              = :user OR " <<
              "#{quoted_table_name}.access = 'Public'", :user => options[:user] || User.current_user).
        order(options[:order] || "#{quoted_table_name}.id DESC").
        limit(options[:limit]) # nil selects all records
      }

 rake aborted!
 /Users/tamer/Sites/fat_free_crm/lib/fat_free_crm/permissions.rb:45: syntax error, unexpected '=', expecting '|'
      scope :my, lambda { |options = {}|
                                    ^
/Users/tamer/Sites/fat_free_crm/lib/fat_free_crm/permissions.rb:53: syntax error, unexpected '}', expecting kEND

Line 45 is the first line

scope :my, lambda { |options = {}|

Do i need to use ruby 1.9*

No, this wont' work in 1.8.7. Yes, it will work in 1.9.2. The more flexible block arguments were introduced as part of Ruby 1.9, including default arguments.

That said, this scope should really just be pushed off into a class method:

def self.my(options = {})
  includes(:permissions).
  where("#{quoted_table_name}.user_id     = :user OR " <<
        "#{quoted_table_name}.assigned_to = :user OR " <<
        "permissions.user_id              = :user OR " <<
        "#{quoted_table_name}.access = 'Public'", :user => options[:user] || User.current_user).
  order(options[:order] || "#{quoted_table_name}.id DESC").
  limit(options[:limit]) # nil selects all records
end

Same result, only it's 1.8.7 compatible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM