mongoid embeds_many 关联集合保持为空
mongoid embeds_many associated collection remain empty
我有两个模型
class Supplier < User
include Mongoid::Document
embeds_many :images
accepts_nested_attributes_for :images
end
class Image
include Mongoid::Document
embedded_in :supplier
end
当我以嵌套形式保存图像时,它会嵌入供应商集合中,即
s = Supplier.first
s.images #some Image records
但问题是图像集本身仍然是空的,即
Image.count # gives 0
您的 Image
模型的文档存储在您的 Supplier
模型的文档中。所以基本上没有在 mongo 中创建名称为 images
的 collection。在您的 mongo 控制台中检查。你只会有 suppliers
collection 而没有 images
collection.
如果你想直接访问图像而不访问特定的你可以这样做
Supplier.all.pluck(:images)
#It will give you an array of all images
或实施has_many
class Supplier < User
include Mongoid::Document
has_many :images
accepts_nested_attributes_for :images
end
class Image
include Mongoid::Document
belongs_to :supplier
end
我有两个模型
class Supplier < User
include Mongoid::Document
embeds_many :images
accepts_nested_attributes_for :images
end
class Image
include Mongoid::Document
embedded_in :supplier
end
当我以嵌套形式保存图像时,它会嵌入供应商集合中,即
s = Supplier.first
s.images #some Image records
但问题是图像集本身仍然是空的,即
Image.count # gives 0
您的 Image
模型的文档存储在您的 Supplier
模型的文档中。所以基本上没有在 mongo 中创建名称为 images
的 collection。在您的 mongo 控制台中检查。你只会有 suppliers
collection 而没有 images
collection.
如果你想直接访问图像而不访问特定的你可以这样做
Supplier.all.pluck(:images)
#It will give you an array of all images
或实施has_many
class Supplier < User
include Mongoid::Document
has_many :images
accepts_nested_attributes_for :images
end
class Image
include Mongoid::Document
belongs_to :supplier
end