Why are Rails model association results not naturally ActiveRecord::Relations?

前端 未结 2 1953
执笔经年
执笔经年 2020-12-16 00:20

I\'m using Rails 3.2.0

Let\'s say I have:

class Comment < ActiveRecord::Base
  has_many :articles
end

c1 = Comment.last

then

2条回答
  •  萌比男神i
    2020-12-16 01:01

    Because when you define association, it places in your model:

    def #{name}(*args)
      association(:#{name}).reader(*args)
    end
    

    .reader() returns AssociationProxy, which removes the .class method and delegates unknown methods to @target through .method_missing.

提交回复
热议问题