繁体   English   中英

ruby 1.8.7 的这种语法是否正常

[英]IS this syntax ok for ruby 1.8.7

我有这个应用程序,在执行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

第 45 行是第一行

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

我需要使用 ruby 1.9*

不,这在 1.8.7 中不起作用。 是的,它将在 1.9.2 中工作。 更灵活的块 arguments 作为 Ruby 1.9 的一部分引入,包括默认的 arguments。

也就是说,这个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

结果相同,只是它与 1.8.7 兼容。

暂无
暂无

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

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