Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Item
- include Mongoid::Document
- field name, type: String
- end
- class Pack
- include Mongoid::Document
- end
- class Pack
- include Mongoid::Document
- field items, type: Array
- end
- a = Pack.new
- a.items = []
- a.items << {item: Item.first, quantity: 4}
- a.save
- NoMethodError: undefined method `__bson_dump__' for #<Item:0x007faf1a56d670>
- a = Pack.new
- a.items = []
- a.items << {item_id: Item.first.id, quantity: 4}
- a.save
- a.items[0].item.name
- class Item
- include Mongoid::Document
- field name, type: String
- belongs_to :item_quantity
- end
- class ItemQuantity
- include Mongoid::Document
- has_one :item
- belongs_to :pack
- field quantity, type: Integer
- end
- class Pack
- include Mongoid::Document
- has_many :item_quantity
- end
- class Item
- include Mongoid::Document
- has_many :item_instances
- end
- class ItemInstance
- include Mongoid::Document
- belongs_to :item
- belongs_to :pack
- end
- class Pack
- include Mongoid::Document
- has_many :item_instances
- end
- pack = Pack.new
- item = Item.first
- pack.item_instances.build(item_id: item.id)
- pack = Pack.new
- product = Product.first
- pack.items.build(product_id: product.id)
- class Pack
- def new_item(product)
- self.items.build(product_id: product.id)
- end
- def products
- self.items.map { |i| i.product }
- end
- end
- pack.add_item(product)
- # Same as using pack.items.build(product_id: product.id)
- pack.products
- # Returns an array (including duplicates) of all the products included in the pack.
Add Comment
Please, Sign In to add comment