简体   繁体   English

ruby 1.8.7 的这种语法是否正常

[英]IS this syntax ok for ruby 1.8.7

i have this app and while doing a rake db:migrate I get this error我有这个应用程序,在执行rake db:migrate时出现此错误

      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第 45 行是第一行

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

Do i need to use ruby 1.9*我需要使用 ruby 1.9*

No, this wont' work in 1.8.7.不,这在 1.8.7 中不起作用。 Yes, it will work in 1.9.2.是的,它将在 1.9.2 中工作。 The more flexible block arguments were introduced as part of Ruby 1.9, including default arguments.更灵活的块 arguments 作为 Ruby 1.9 的一部分引入,包括默认的 arguments。

That said, this scope should really just be pushed off into a class method:也就是说,这个scope真的应该被推到 class 方法中:

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.结果相同,只是它与 1.8.7 兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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