[英]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.