Ruby on rails ApplicationRecord关联方法查找是如何工作的?

Ruby on rails ApplicationRecord关联方法查找是如何工作的?,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,正如我所理解的Ruby继承和方法查找,当一个子实例调用一个父实例的实例方法(该方法反过来调用一个在父实例和子实例中都命名的方法)时,作用域仍然在子实例中。因此,这将发生: class Foo def method1 "foo" end def method2 puts method1 end end class Bar < Foo def method1 "bar" end end Bar.new.method2 => "b

正如我所理解的Ruby继承和方法查找,当一个子实例调用一个父实例的实例方法(该方法反过来调用一个在父实例和子实例中都命名的方法)时,作用域仍然在子实例中。因此,这将发生:

class Foo 

  def method1
    "foo"
  end

  def method2
    puts method1
  end
end

class Bar < Foo

  def method1
    "bar"
  end
end

Bar.new.method2
=> "bar"
class-Foo
定义方法1
“福”
结束
定义方法2
方法1
结束
结束
类Bar“巴”
然而,当我对ActiveRecord关联做我认为类似的事情时,我并没有得到我所期望的:

class Foo < ApplicationRecord

  has_many :orders
  has_many :order_items, through: :orders
end

class Bar < Foo

  has_many :orders, -> { where(attribute1: 1) }
end
class Foo{where(attribute1:1)}
结束

当我调用
bar.orders
时,我得到了我所期望的。但是当我调用
bar.order\u items
时,我得到的结果与调用
foo.order\u items
时的结果相同(不使用查询范围)。如果我在bar.rb中包含
有\u多个:订单\u项,通过::订单
,它的行为与我预期的一样。为什么ApplicationRecords的行为是这样的?我是在比较苹果和橙子吗?

就像评论中的Max states一样,在使用元编程时,您不是在定义方法,而是在调用方法,因此不涉及继承。

我是在比较苹果和橙子吗?-是-第一个例子是实例方法的继承,而
有很多:orders
是对类本身进行操作的元编程。