user1011444
user1011444

Reputation: 1399

Cannot save date in hash (Mongid, Rails)

I'm using mongoid as the orm for my mongodb backend in rails. I can create an object (notice that release_date is a date object):

movie1 = Movie.create(title: "The Dark Knight", release_date: "2012-03-03")
=> #<Movie _id: 515490b884322b14e2000002, _type: "Movie", title: "The Dark Knight", release_date: 2012-03-03 00:00:00 UTC> 

But then when I try to use it with movie.release_date to put it in a hash, it outputs the date as a string, and I can't save my hash:

list.data[1] = {id: movie2.id, title: movie2.title, release_date: movie2.release_date}
 => {:id=>"515490ce84322b14e2000003", :title=>"The Matrix", :release_date=>Sat, 04 Apr 1998}

Any kind of help would be much appreciated, thanks again.

Upvotes: 1

Views: 141

Answers (1)

user324242
user324242

Reputation:

That's no string - Ruby just called inspect on each item of the hash to show you a human readable value. Note that there are no " around the date.

class Movie 
    include Mongoid::Document

    field :release_date, type: Date
end

movie1 = Movie.create(title: "The Dark Knight", release_date: "2012-03-03")
hash = {release_date: movie1.release_date}
p hash[:release_date].class # => Date

Upvotes: 1

Related Questions