Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 委托找不到父对象_Ruby On Rails_Delegation - Fatal编程技术网

Ruby on rails 委托找不到父对象

Ruby on rails 委托找不到父对象,ruby-on-rails,delegation,Ruby On Rails,Delegation,我有这门课 class Email < ActiveRecord::Base belongs_to :order delegate :foo, to: :order 然而,当我将这种方法添加到电子邮件中时,它突然起了作用 def order Order.find( order_id ) end 为什么会发生这种情况?我很高兴它现在能工作,但我不明白它为什么会失败。我在一个对我来说是新的代码库中工作,以前没有在我自己的项目中使用过委托 谢谢我当然看不出会有什么问题,我

我有这门课

class Email < ActiveRecord::Base    
  belongs_to :order
  delegate :foo, to: :order
然而,当我将这种方法添加到电子邮件中时,它突然起了作用

def order
  Order.find( order_id )
end
为什么会发生这种情况?我很高兴它现在能工作,但我不明白它为什么会失败。我在一个对我来说是新的代码库中工作,以前没有在我自己的项目中使用过委托


谢谢

我当然看不出会有什么问题,我很想知道你会发现什么。由于现在每次调用#Order时都会运行一个新的查询并获得一个新的Order实例,因此您的解决方案不是很好。您确定您正在测试的电子邮件对象具有关联的Order吗?您可以添加一个
validates:order\u id,presence:true
来强制建立关系
def order
  Order.find( order_id )
end