Hi people,

i’m new to rails and have difficulties in actively using the model relationships.
I’m building an onlinestore with rails 4.2.4 following the book agile development with rails 4.
The asignment is to create a migration that copies the product price to the line item.

Class Product < ActiveRecord::Base

has_many :line_items

Class LineItem < ActiveRecord::Base (is a jointable between product and cart)

belongs_to :product

My question is how do i instruct rails to copy an attribute from one model to the other (in this case product.price to line_item.price)

I understand that the belongs_to and has_many methods bring in new methods when called.
@line_item = @product.line_items
I somehow can’t make pratical use of this knowledge!! What really confuses me is these are instance_variable
of the models but when it comes to use the author has been using the tablenames
eg :line_items, :products etc how i’m i supposed to copy the attribute_price from product using instance_variables?

My migration for the new added price column to LineItem model

class AddPriceToLineItem < ActiveRecord::Migration
def up
add_column :line_items, :price, :decimal, precision: 8, scale: 2

If the question is not clear just let me whats confusing.

Thanks for help

I think you not should do that. If you store price in products table, why you are store same thing in another table?

Андрей Молчанов is correct when he says that you should not store the
same information in two places in the database. However there are
certainly cases where it is necessary to adjust data in existing
tables during a migration. Have a look at [1] which has examples of
how to do this and notes some pitfalls to watch out for.

It seems that section has been removed from the latest guides, I don't
know why. Anyone know whether it was removed for a good reason?



It was removed here:

That is interesting. It appears that because no-one could come up
with a good example of how using the model can go wrong the whole
section was removed, meaning that the guide has no example of how to
use a model in a migration. Not entirely sure I would agree with that


@Андрей Молчанов
I have a method add_product in the cart_model that’s supposed to capture
the price from line_item. Within the new migration AddPriceToLineItem i was given the hint
to iterate over the LineItem.all.each do |item|

 add the product_price to each item in LineItem


If the price is being parsed around objects doesn’t it make sense to monitor it?
What if the cart has problems obtaining the price properly?


Please quote the relevant parts of the previous message when replying.
Since you have not done so it is not easy to see which points you are
commenting on.

I don't know what you mean by "parsed around objects" I guess you
probably meant "passed around objects" but even then I don't know what
you mean, and I also don't know what you mean by "monitor it".
Also you ask what if the cart has problems obtaining the price
properly, again I don't know what you mean.

Please try again to ask the questions.