Agile Web Development with Rails 8_B6.0(page 126)

Code shows:

@line_item = @cart.line_items.build(product: product)

This produces an error of " * Product must exist" when you press the “Add to Cart” button.

However, if I change the line to add “.id” to product, then it works as expected:

@line_item = @cart.line_items.build(product_id: product.id)

Weird. It works for me. Furthermore, product won’t have an id unless it exists.