On 7 October 2015 at 00:26, peter williams <
sto...@gmail.com> wrote:
> 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)
Андрей Молчанов 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?
Colin
[1]
http://guides.rubyonrails.org/v3.2.8/migrations.html#using-models-in-your-migrations