small medium large xlarge

For facing_pragsmall
25 Jul 2012, 15:04
Vasili Chyrvon (8 posts)

Hi everyone,

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.

Samr_small_pragsmall
24 Jul 2012, 12:56
Sam Ruby (634 posts)

The using of update_attribute() removes this problem</blockquote>

I’m not certain I understand the question, as the example you provided uses update_attribute.

Check to make sure that you do a @save@ at some point after every assignment. @update_attribute@ will do a save for you.

For facing_pragsmall
25 Jul 2012, 16:11
Vasili Chyrvon (8 posts)

Oops, I accidentally posted the corrected code. Sorry.

There was meant to be @li.price = li.product.price@ insted of @li.update_attribute(:price, li.product.price)@ in my first post. I will correct it right now.

I was trying to solve this problem, and even tried with the depot_j. I took away this migration, run rake db:migrate, then add couple of items in the cart and add a code mentioned above in the /store/index.html.erb. And then I’ve returned the migration back and ran it.

But the result was the same… false, false false. It just doesn’t want to add the values to the price!

I was saving the migration after any change in it.

Or you meant to add @li.save@ after the assignment in the block?

For facing_pragsmall
25 Jul 2012, 16:17
Vasili Chyrvon (8 posts)

I’ve tried with @li.save@ and it works. It do copies the prices.

But why the same migration in the source of depot_j don’t include this @.save@?

Samr_small_pragsmall
25 Jul 2012, 16:50
Sam Ruby (634 posts)

Create also does a save. Unfortunately, depending on what printing you have, the scenario in the book was tested against version 3.2.0 and my guess is that you are running with 3.2.3 or later, and there was a change in version 3.2.3 that broke this step.

In a few days, there should be a new version available (free to people who purchased ebook versions) that works with 3.2.6 (and even 3.2.7rc1). Over time, this new version will be making its way to places like Amazon and book shelves.

For facing_pragsmall
25 Jul 2012, 17:19
Vasili Chyrvon (8 posts)

Thanks alot for your explanations and for the advice! Now I’ll remember to @save@ ))

You must be logged in to comment