josedonizetti / ruby-duration

Immutable type that represents some amount of time with accuracy in seconds.
http://bit.ly/ruby-duration
MIT License
124 stars 23 forks source link

Is it working with Mongoid 4? #19

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi,

I'm trying to assign duration to a field. Using Rails 4.1 and Mongoid 4 Is it not working with this stack or am I doing something wrong? Here is what I do:

require 'duration/mongoid'
class Price
  include Mongoid::Document

  embedded_in :workdone 
  field :variation_id   
  field :price, type: Float
  field :discount, type: Integer
  field :duration, type: Duration
  price = workdone.prices.build
          price.variation_id =Moped::BSON::ObjectId.from_string(var._id )  
          pricelow = Price.get_high_lows(price.variation_id ,self)          
          price.price =  pricelow.present? ? pricelow[0].price : var.base_price.to_f
          price.discount = pricelow.present? ? pricelow[0].discount : 0
          price.duration = Duration.new(:minutes=>60)
          price.save!
...

=> #<Price _id: 53ada69963616e7250140000, variation_id: BSON::ObjectId('53a4063a63616e291c3e0000'), price: 19.0, discount: 0, duration: nil>
peleteiro commented 10 years ago

I really don't know. I don't use Rails nor mongo nowadays. Sorry. If you can figure it out and fix it. I can add you as a committer.

ghost commented 10 years ago

ok I'll take a look

exocode commented 10 years ago

I got it working with this:

require 'duration/mongoid'
class Price
  include Mongoid::Document
  field :duration, type: Duration
end

price = Price.new
price.duration = 90

price.duration.minutes # => 1
price.duration.seconds # => 30

Think you can close it.

I use mongoid 3