I have the problem with assigning the product price to the line items.
My migration code looks like
class AddPriceToLineItems < ActiveRecord::Migration def up add_column :line_items, :price, :decimal LineItem.reset_column_information LineItem.all.each do |li| li.price = li.product.price end end def down remove_column :line_items, :price end end
It migrated well, but actually for the already existing line items the price column is empty. I’ve checked this by adding the following code to my store/index.html.erb:
<% LineItem.all.each do |li| %> <%= li.price? %><br> <% end %>
And the result is false false false
Why the assignment isn’t working in my migration?
I spent many hours trying different things and noticed, that the assignment like
<% LineItem.first.price = 11.11 %> didn’t work from view. The second question: Is it normal? Maybe the assigning to models never works from the view?
The using of @update_attribute()@ removes this problem, but I really want to know why this problem appears and how to solve it without changing the @=@ to @update_attribute()@ method.
I will be very appreciated for any comments. Thanks.