Ruby on rails 从ActiveRecord\u Associations\u CollectionProxy对象获取模型引用

Ruby on rails 从ActiveRecord\u Associations\u CollectionProxy对象获取模型引用,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有两个活动记录模型 class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecordd:Base end 现在当我 user.posts.class.to_s # Post::ActiveRecord_Associations_CollectionProxy 有没有一种方法可以让我无需“拆分”字符串即可获得Post::“然后将其固定 使用model或klass属性-它将返回您要查找

我有两个活动记录模型

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecordd:Base
end
现在当我

user.posts.class.to_s 
# Post::ActiveRecord_Associations_CollectionProxy

有没有一种方法可以让我无需“拆分”字符串即可获得Post::“然后将其固定

使用
model
klass
属性-它将返回您要查找的常量:

user.posts.model
#=> Post
user.posts.klass
#=> Post
有关更多详细信息,请参阅

user.posts.model
#=> Post
user.posts.klass
#=> Post