ruby on rails - Mongoid Query Related Collection -
i'm trying figure out how query related collections in mongoid.
here object model (simplified brevity):
category ------------------- class category include mongoid::document field :name, type: string ... belongs_to: product end class product include mongoid::document field :name, type: string ... has_one :category end i'm trying build query products category particular name e.g. "toy"
products = product.where('category.name' => 'toy') i nil collection back. think mongoid not support types of queries directly. how build query accomplish that?
try following
category = category.find_by(name: 'toy') # following work products = product.where(category: category) products = product.where(category_id: category.id) this work, but it's not recommended use mongoid way
mongoid provides relational-style associations convenience application developers used dealing relational databases, not recommend use these extensively.
Comments
Post a Comment