small medium large xlarge

20 Apr 2013, 14:30
Louis Rhys (22 posts)

In a model with foreign key, for example LineItem (which contains a FK to product), it seems that I can set both product and product_id

How does this work? What will happen if I set conflicting values to these two? Which way is normally recommended?

20 Apr 2013, 17:44
Sam Ruby (634 posts)

LineItem only has a product_id. If you assign a product, Rails will automatically extract the id from the product object specified, and place the value in product_id.

Use of the “reader” form is quiet common as it allows convenient traversal (e.g. item.product.title). Use of the writer is a matter of personal preference.

You must be logged in to comment