Ruby on Rails Tuesday, October 6, 2015

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

--
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 rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e897acfa-e67f-4041-98ec-1b30d4c71121%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment