ActiveRecordAssociation

35 views
Skip to first unread message

peter williams

unread,
Oct 7, 2015, 1:57:00 AM10/7/15
to Ruby on Rails: Talk
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.
e.g
@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
    end
  end

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

Thanks for help

Андрей Молчанов

unread,
Oct 7, 2015, 2:52:15 AM10/7/15
to Ruby on Rails: Talk
I think you not should do that. If you store price in products table, why you are store same thing in another table?

среда, 7 октября 2015 г., 8:57:00 UTC+3 пользователь peter williams написал:

Colin Law

unread,
Oct 7, 2015, 3:32:58 AM10/7/15
to Ruby on Rails: Talk
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

Frederick Cheung

unread,
Oct 7, 2015, 7:03:55 AM10/7/15
to Ruby on Rails: Talk

Colin Law

unread,
Oct 7, 2015, 8:22:38 AM10/7/15
to Ruby on Rails: Talk
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
logic.

Colin

peter williams

unread,
Oct 7, 2015, 9:30:00 AM10/7/15
to Ruby on Rails: Talk
@Андрей Молчанов
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

end

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?

thx


Colin Law

unread,
Oct 7, 2015, 11:10:56 AM10/7/15
to Ruby on Rails: Talk
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.

Colin
Reply all
Reply to author
Forward
0 new messages