mongoid money field

This is a super simple gem to use RubyMoney money type columns with mongoid

github.com/RubyMoney/money github.com/mongoid/mongoid

Description

A simple gem that creates a Money datatype using RubyMoney for Mongoid.

Inspired by gist.github.com/840500

Installation

Include the gem in your Gemfile

gem 'mongoid_money_field'

Usage

class DummyMoney
  include Mongoid::Document
  include Mongoid::MoneyField

  field :description

  money_field :cart_total # defaults to 0
  money_field_without_default :price, :old_price # defaults to nil

  # to disallow changing currency (and to not store it in database)
  money_field_with_options :price2, fixed_currency: 'GBP'

  # set a default
  money_field_with_options :price3, default: '1.23 RUB'
end

All Money values are converted and stored in mongo as cents and currency in two separate fields.

Finding by price

DummyMoney.where(price_cents: 123).first

Copyright © 2012 glebtv. MIT License.