简体   繁体   English

什么是 <ActiveRecord::Associations::CollectionProxy []> ?

[英]What is <ActiveRecord::Associations::CollectionProxy []>?

I have these models: 我有以下模型:

class Item < ActiveRecord::Base
    has_many :item_categoryships
    has_many :categories, class_name: 'ItemCategoryship', foreign_key: 'category_id', :through => :item_categoryships

    belongs_to :user

    validates :title, presence: true

end 结束

class Category < ActiveRecord::Base
    has_many :item_categoryships
    has_many :items, :through => :item_categoryships

    belongs_to :user

    validates :name, presence: true
end


class ItemCategoryship < ActiveRecord::Base
    belongs_to :item
    belongs_to :category
    validates :item_id, presence: true
    validates :category_id, presence: true
end


class User < ActiveRecord::Base
    has_many :items
    has_many :categories, class_name: 'Category'
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :confirmable, :async
end

But I found when I call item.categories will get a empty array !!! 但是我发现当我调用item.categories时会得到一个空数组! I have checked database, there is a record here. 我已经检查了数据库,这里有一条记录。

When I test in the rails console, I didn't get any record back, just saw 'ActiveRecord::Associations::CollectionProxy []'. 当我在Rails控制台中测试时,我没有得到任何记录,只是看到了“ ActiveRecord :: Associations :: CollectionProxy []”。

What is this? 这是什么? I am using Rails 4.0.2. 我正在使用Rails 4.0.2。

Thanks you all. 谢谢大家

ActiveRecord::Associations::CollectionProxy is ActiveRecord class for collection associations. ActiveRecord::Associations::CollectionProxy是集合关联的ActiveRecord类。 Now, your code should work if you change line in Item class describing categories association to: 现在,如果您将描述categories关联的Item类中的行更改为:

has_many :categories, through: :item_categoryships

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActiveRecord :: Associations :: CollectionProxy - ActiveRecord::Associations::CollectionProxy # <ActiveRecord::Associations::CollectionProxy []> 在Rails中 - #<ActiveRecord::Associations::CollectionProxy []> in Rails 访问ActiveRecord :: Associations :: CollectionProxy中的属性 - Access attributes in ActiveRecord::Associations::CollectionProxy ActiveRecord :: Associations :: CollectionProxy上的Rails方法 - Rails method on ActiveRecord::Associations::CollectionProxy ActiveRecord_Associations_CollectionProxy问题 - ActiveRecord_Associations_CollectionProxy issue Rails 3 HABTM产生ActiveRecord :: Associations :: CollectionProxy对象 - Rails 3 HABTM yields ActiveRecord::Associations::CollectionProxy objects 在Capybara中伪装ActiveRecord :: Associations :: CollectionProxy与Mocha(Rails) - Faking ActiveRecord::Associations::CollectionProxy with Mocha in Capybara (Rails) Rails循环通过ActiveRecord :: Associations:CollectionProxy - Rails loop through ActiveRecord::Associations:CollectionProxy 表导轨中ActiveRecord :: Associations :: CollectionProxy的排序顺序 - Sort order for ActiveRecord::Associations::CollectionProxy in table rails ActiveRecord :: Associations :: CollectionProxy HABTM创建/构建不起作用 - ActiveRecord::Associations::CollectionProxy HABTM create/build not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM