这个语法对于ruby 1.8.7是好的

时间:2011-08-12 19:20:47

标签: ruby-on-rails ruby ruby-on-rails-3

我有这个app并在执行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 个答案:

答案 0 :(得分:1)

不,这不会在1.8.7中起作用。是的,它将在1.9.2中有效。作为Ruby 1.9的一部分引入了更灵活的块参数,包括默认参数。

也就是说,这个scope应该被推到一个类方法中:

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兼容。