Agile Web Development with Rails 6: LineItem

Hi,
Newbie here trying to learn Ruby on rails with AWDR6. Got a question regarding the LineItem scaffolded as part of Iteration D2: Connecting Products to Carts. What additional value does it provide? Is it just a join table for the many to many relationship between products and carts? But then a has_many association was used instead of has_many: through or has_many_and_belongs to as per docs. I am struggling to understand this bit of the tutorial as I do not get the underlying logic so any help will be highly appreciated

In theory if the join table doesn’t do more than just connect two tables, you are right, you probably would not need to create a model class for it. Iteration D2 doesn’t really foreshadow this, but in Iteration E1, we add a quantity to the line item, and this is a good example of why you’d make a model for the join table - to add metadata about the many-to-many relationship itself.

1 Like

has_many is the correct association it is not many to many relationship. A cart has many line items and a line item has one product. I think the bug is in the add_product method. If there is no line_item it builds a new one with a product but it left off the quantity of 1.