NoMethodError:未定义的方法“ liquidity_manager?”对于#<order:0x007fd9b7a4cf98>

时间:2018-12-05 08:58:22

标签: ruby-on-rails ruby ruby-on-rails-4

我的解决方案抛出NoMethodError。这是我在下面所做的:

Order.rb

class Order < ActiveRecord::Base
  belongs_to :member
end

Member.rb

class Order < ActiveRecord::Base
  has_many :orders

  def liquidity_manager?
    @is_liquidity_manager ||= self.class.liquidity_managers.include?(self.email)
  end

  def liquidity_managers
    ENV['LM_ACCOUNTS'].split(',')
  end
end

耙任务逻辑

lib / update_order_tags.rake

中的逻辑
num_counts = (Order.count/10).ceil
num_counts.times do |i|
  Order.all.offset(i*10).limit(10).find_each do |g|
    if g.tags.blank? # am saving on only the blank fields
      if g.liquidity_manager? # The error is here
        g.tags = 'LM'
        g.save!
      else
        g.tags = 'Customer'
        g.save!
      end
    end
  end
end

我如何引用liquidity_manager?方法在lib中对我有用?

2 个答案:

答案 0 :(得分:1)

您在Order#liquidity_manager?中定义了Member.rb,Rails不会自动加载它。您必须显式加载文件Member.rb,或将该文件重命名为/app/models/order.rb

答案 1 :(得分:1)

因此,根据@sawa在回答部分中给我的建议,这是我为使之起作用所做的工作。

num_counts = (Order.count/10).ceil
num_counts.times do |i|
  Order.all.offset(i*10).limit(10).find_each do |g|
    if g.tags.blank? 
      if g.member.liquidity_manager? # So this easily gets to Member.rb to reference liquidity_manager? method.
        g.tags = 'LM'
        g.save!
      else
        g.tags = 'Customer'
        g.save!
      end
    end
  end
end