On 7 October 2015 at 00:26, peter williams <[email protected]> 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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuBaT6rKNU_eNybA4WtT_efMtVSzPW1Lxqp7DyHk3h%2Bzg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to