> For the complete documentation index, see [llms.txt](https://allfutures.leastbad.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://allfutures.leastbad.com/examples/embedded-associations.md).

# Embedded associations

{% hint style="warning" %}
Experimental — API may still move.
{% endhint %}

```ruby
class Cart < AllFutures::Base
  embeds_many :items, dependent: :destroy, autosave: true
end

class Item < AllFutures::Base
  attribute :cart_id
  attribute :sku, :string
  attribute :qty, :integer, default: 1
  embedded_in :cart
end

cart = Cart.new
cart.items.build(sku: "duck-001", qty: 2)
cart.items.build(sku: "duck-002")
cart.save

# later request — children lazy-load on first touch
cart = Cart.find(cart.id)
cart.items.map(&:sku) # => ["duck-001", "duck-002"]
```

Children can also be created on their own if you set the foreign key; they still appear when the parent is loaded:

```ruby
Item.create(cart_id: cart.id, sku: "duck-003")
Cart.find(cart.id).items.size # => 3
```
