3

my users can input and create prices. I wanted to know how to make it so when users input 23.5 they get 23.50 instead or 0.0 they get 0.00. How does one add to the t.decimal or my price:decimal the following ability?

Thank you for the help!

The Answer (:price with scale and precision)

class CreatePrices < ActiveRecord::Migration
  def self.up
    create_table :prices do |t|
      t.string :price_name
      t.decimal :price, :precision => 10, :scale => 2
      t.date :date

      t.timestamps
    end
  end

  def self.down
    drop_table :prices
  end
end

Schema.rb:

  create_table "prices", :force => true do |t|
    t.string   "price_name"
    t.decimal  "price",      :precision => 10, :scale => 2
    t.date     "date"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
  end

my scaffolded form: <%= f.label :price %>
<%= f.text_field :price %>

Then in your view put number_to_currency(@model.attribute):

Price:

LearningRoR
  • 26,582
  • 22
  • 85
  • 150
  • 1
    Seems like a output problem. Use `number_to_currency(price)` to display the output. Show the part of the code where the price is displayed. – Zabba Jun 06 '11 at 18:04

3 Answers3

5

Try using the :scale option in your migration. Use a scale of 2 if you want two digits to the right of the decimal point, e.g:

 t.decimal :price, :precision => 10, :scale => 2
mbreining
  • 7,739
  • 2
  • 33
  • 35
  • My migration is old, is it still OK to add to it? I put it inside of it and did a db:reset, added the price 23.50 and still got 23.5. – LearningRoR Jun 05 '11 at 19:03
  • 1
    It's ok as long as you're willing to recreate your table (and thus lose the data that's currently in your database) since you'll need to run `rake db:migrate:reset` to re-run all the migrations. Otherwise simply create a new migration to change the datatype of your price column, e.g. `t.change_column :products, :price, :decimal, { :scale => 2, :precision => 10 }`. See this [link](http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_column) for more info on `change_column`. – mbreining Jun 05 '11 at 19:16
  • 1
    Also check out this [post](http://stackoverflow.com/questions/2450494/rails-cannot-add-precision-or-scale-options-with-change-column-in-a-migration) if you're using SQLite3. – mbreining Jun 05 '11 at 19:16
  • Yes i am using SQLite3, how would the change column migration look? i get lost at rails generate migration ChangePrice------. – LearningRoR Jun 05 '11 at 19:35
  • 1
    See [this](http://stackoverflow.com/questions/2450494/rails-cannot-add-precision-or-scale-options-with-change-column-in-a-migration) and [this](http://stackoverflow.com/questions/2083543/modify-a-columns-type-in-sqlite3/2083562#2083562) – Zabba Jun 05 '11 at 19:45
  • @Zabba Those threads make little if any sense to me, I'm to brand new at Ruby-on-Rails. it says something about not even doing a migration, then what do i do? – LearningRoR Jun 06 '11 at 15:25
  • I think i got it working by adding inside of the migration like Feelnoway said to do but i dont see any changes when i create a price. – LearningRoR Jun 06 '11 at 15:30
4

Without using precision and scale, I did it like this:

def price #in price model to set price field
 number_to_currency(self[:price], :unit => '')
end

Migration as:

t.float :price, :default => '1.00'

View as:

f.text_field :price
Abhaya
  • 2,086
  • 16
  • 21
1

I ended doing the scale and precision but also adding in the view the number_to_currency method.

LearningRoR
  • 26,582
  • 22
  • 85
  • 150