0

Have added money-rails gem to my app.

Migrated its column to my model Item

Migration

class AddPriceToItems < ActiveRecord::Migration[5.0]
  def change
    add_column :items, :price, :money
  end
end

Model Item

class Item < ApplicationRecord
  belongs_to :invoice

  validates_numericality_of :unit_price, :quantity 

  monetize :price_cents

end

Schema

ActiveRecord::Schema.define(version: 20170223211329) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "invoices", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "items", force: :cascade do |t|
    t.decimal  "unit_price", precision: 10, scale: 2
    t.integer  "quantity"
    t.integer  "invoice_id"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.money    "price",                     scale: 2
    t.index ["invoice_id"], name: "index_items_on_invoice_id", using: :btree
  end

  add_foreign_key "items", "invoices"
end

This is the error that I'm running into

undefined method `price_cents' for #<Item:0x007f9f6b366ae8>
Did you mean?  price_cents=

Not sure how to resolve this.

EDIT

Ok, I dropped the price column, and have added it back as price_cents column as integer ...

However, should it be this in the schema:

t.integer "price_cents" ..

OR

t.monetize "price_cents"

user273072545345
  • 1,536
  • 2
  • 27
  • 57

3 Answers3

1

The name of your column needs to be price_cents not price.

The type of your column needs to be integer.

I think you tried to copy

add_money :products, :price

but you used add_column instead of add_money.

I used this gem recently but the add_money helper was undefined so I used

add_column :items, :price_cents, :integer

I don't think it will work with a money column type

daslicious
  • 1,535
  • 10
  • 18
  • 1
    ... again, in other words, drop the original `price` column and add it back as `price_center` and as integer ... ? I'm kind of confused about what I did ... not sure how I did `money` column instead of `monetize` since that's for Rails 4x and up ... – user273072545345 Feb 24 '17 at 20:00
  • `t.integer "price_cents"` – daslicious Feb 24 '17 at 23:13
1

If you are trying to make sure that the money amount you are adding/multiplying doesn't give inaccurate sums or products (like $14.099..), then you may consider going this path:

1) When generating your Items model use this price:monetize

2) In your migration file, this will reflect as: t.monetize :price

3) After running rails db:migrate, this will produce two columns in your ItemsTable: The first column will be price_cents ( which will be the amount of the item in cents, (eg 1000 = $10) The second column will be price_currency (e.g. $), the default is USD in the schema.rb

t.integer "price_cents", default: 0, null: false t.string "price_currency", default: "USD", null: false

Anna S
  • 157
  • 1
  • 9
0

Your model should have an integer (not money type) price_cents column, not price. The symbol passed to monetize should be that column. See here: https://github.com/RubyMoney/money-rails#usage-example

Teoulas
  • 2,943
  • 22
  • 27