模型实例方法在第一次运行时返回错误结果,随后返回正确结果

时间:2018-07-15 11:01:38

标签: ruby-on-rails

我有三种与has_many相关的模型:通过关联:

class Account < ApplicationRecord
  has_many :account_owners
  has_many :employees, through: account_owners

  def is_owned_or_belongs_to_team_of_employees(employee)
    employee.team.any? { |m| employees.include?(m) }
  end
end

class AccountOwner < ApplicationRecord
  belongs_to :account
  belongs_to :employee
end

class Employee < ApplicationRecord
  has_many :account_owners
  has_many :accounts, through: :account_owners

  def team
    self.class.where(
      'id IN (?)',
      self. class.find_by_sql(['WITH RECURSIVE search_tree(id, path) AS (
                                  SELECT id, ARRAY[id]
                                    FROM employees
                                    WHERE id = ?
                                  UNION ALL
                                  SELECT employees.id, path || employees.id
                                    FROM search_tree
                                    JOIN employees ON employees.manager_id = search_tree.id
                                    WHERE NOT employees.id = ANY(path)
                                )
                                SELECT id FROM search_tree ORDER BY path',
                             self.id])
    ).order(:id)
  end
end

我正在开发环境中的Rails控制台中(使用我第一次加载到数据库中的一些固定装置)手动测试Account#is_owned_or_belongs_to_team_of_employees方法。

当我在控制台中运行该方法时,会发生以下情况:

> a = Account.first
 => #<Account id: 534788375, name: "Sales Rep 1 (elena)-owned account", code: "EEE", created_at: "2018-07-15 09:41:55", updated_at: "2018-07-15 09:41:55">
> e = Employee.find_by(first_name: 'Elena')
 => #<Employee id: 701979064, first_name: "Elena", last_name: "López", manager_id: 1069403509, created_at: "2018-07-15 09:41:55", updated_at: "2018-07-15 09:41:55", mobile: nil, work: nil>
> e.team
 => #<ActiveRecord::Relation [#<Employee id: 701979064, first_name: "Elena", last_name: "López", manager_id: 1069403509, created_at: "2018-07-15 09:41:55", updated_at: "2018-07-15 09:41:55", mobile: nil, work: nil>]>
> a.is_owned_or_belongs_to_team_of e
 => nil
> a.is_owned_or_belongs_to_team_of e
 => true

如您所见,该方法第一次返回nil(错误!),随后又返回true(正确!)。

令人惊奇的是,如果我定义如下方法,我可以纠正问题:

def is_owned_or_belongs_to_team_of employee
  puts "employees are #{employees.inspect}"
  employee.team.any? { |m| employees.include?(m) }
end

现在执行正确,并且该方法始终返回相同的结果(在我的示例中为true

> a = Account.first
 => #<Account id: 534788375, name: "Sales Rep 1 (elena)-owned account", code: "EEE", created_at: "2018-07-15 09:41:55", updated_at: "2018-07-15 09:41:55">
> e = Employee.find_by(first_name: 'Elena')
 => #<Employee id: 701979064, first_name: "Elena", last_name: "López", manager_id: 1069403509, created_at: "2018-07-15 09:41:55", updated_at: "2018-07-15 09:41:55", mobile: nil, work: nil>
> e.team
 => #<ActiveRecord::Relation [#<Employee id: 701979064, first_name: "Elena", last_name: "López", manager_id: 1069403509, created_at: "2018-07-15 09:41:55", updated_at: "2018-07-15 09:41:55", mobile: nil, work: nil>]>
> a.is_owned_or_belongs_to_team_of e
 => true
> a.is_owned_or_belongs_to_team_of e
 => true

如果我删除puts语句,我们回到第一个平方:该方法第一次返回nil,然后返回true

而且,令人惊讶的是,如果我保留puts语句但删除inspect(也就是说,我只是做puts "employees are #{employees}",我们也回到第一个平方:nil第一次,true之后。

有什么主意吗?这是怎么回事?

顺便说一句,我正在运行Ruby 2.5.1和Rails 5.2.0。

1 个答案:

答案 0 :(得分:4)

很高兴我偶然发现了这个独角兽的bug!

调试了几个小时后,我发现了以下内容:

  1. any?rails 5.2 release中进行了新更改,应该将其委派给Enumerable
  2. 令人惊讶的事情是,如果在any?的实现中放入binding.pry并调用super,即使是第一次也返回true,然后该方法返回{{1} }。 nil

  3. 如果您将其添加到~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.0/lib/active_record/relation.rb @ line 228 ActiveRecord::Relation#any?: employee.team,则所有操作均会一致。

  4. 如果您放置.to_a,它将返回true。
  5. 如果您在any? { |_| true }的块内检查值,则返回true,但include?仍返回any?
  6. 如果您避免解决nil关联(通过在块之前调用has_may through),甚至避免在.to_a块内使用其他关联,那么一切都会按预期进行。
  7. 使用任何其他红宝石版本都可以解决此问题。

摘要

此问题是在any?开始包含2.5.1时在ruby v5.2.0导轨ActiveRecord::Relation中引入的。在尝试解决{{ 1}}关联中。