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
Class LineItem < ActiveRecord::Base (is a jointable between product and cart)
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
add_column :line_items, :price, :decimal, precision: 8, scale: 2
If the question is not clear just let me whats confusing.
Thanks for help