为什么不能私下使用该方法?

时间:2018-02-20 08:29:33

标签: ruby

为什么不能在update_message_business_id下使用private方法?

class Payment < ApplicationRecord

 ...

  def self.last_payment_gathering_information(customer_thread_id, employee_user_id, business_id)
    payment = Payment.joins(:message).where("messages.customer_thread_id = ?", customer_thread_id)
       .where("messages.employee_user_id = ?", employee_user_id)
       .where(state: :gathering_information)
       .order(id: :desc).first
    payment.update_message_business_id(business_id) unless payment.nil?
    payment
  end

  private

  def update_message_business_id(business_setting_id)
    business_setting = BusinessSetting.find_by_business_id(business_setting_id)
    self.message.business_setting = business_setting
    self.message.save
  end
end

# => NoMethodError (private method `update_message_business_id' called for Payment:0x00007faac24548d0*

1 个答案:

答案 0 :(得分:3)

直接,这是因为你用显式接收器调用这个方法,而你在Ruby中做不到。

要扩展此答案,您无法在此上下文中真正执行此私有方法调用,因为self中的last_payment_gathering_information(始终为默认接收方)指的是Payment类,不是Payment实例(具有update_message_business_id私有方法)。