Skip to content

Instantly share code, notes, and snippets.

@mrmemes-eth
Created January 20, 2011 14:32
Show Gist options
  • Save mrmemes-eth/787963 to your computer and use it in GitHub Desktop.
Save mrmemes-eth/787963 to your computer and use it in GitHub Desktop.
Confusing Mongoid output - full source from examples below at: https://github.com/voxdolo/ding/tree/mongoid_fabrication
ruby-1.9.2-p0 :026 > s = Session.create
=> #<Session _id: 4d3845f0c1037b05f3000005, created_at: 2011-01-20 14:25:52 UTC, updated_at: 2011-01-20 14:25:52 UTC, name: "aa", custom: nil>
ruby-1.9.2-p0 :027 > s.timers << Pomodoro.new
=> [#<Pomodoro _id: 4d384601c1037b05f3000006, created_at: 2011-01-20 14:26:09 UTC, updated_at: 2011-01-20 14:26:09 UTC, duration: 1500>]
ruby-1.9.2-p0 :028 > s.timers.count
=> 1
ruby-1.9.2-p0 :029 > s.reload
=> #<Session _id: 4d3845f0c1037b05f3000005, created_at: 2011-01-20 14:25:52 UTC, updated_at: 2011-01-20 14:25:52 UTC, name: "aa", custom: nil>
ruby-1.9.2-p0 :030 > s.pomodoros.count
=> 0
ruby-1.9.2-p0 :031 > s.pomodoros << Pomodoro.new
=> [#<Pomodoro _id: 4d38461fc1037b05f3000007, created_at: 2011-01-20 14:26:39 UTC, updated_at: 2011-01-20 14:26:39 UTC, duration: 1500>]
ruby-1.9.2-p0 :032 > s.timers.count
=> 1
ruby-1.9.2-p0 :033 > s.reload
=> #<Session _id: 4d3845f0c1037b05f3000005, created_at: 2011-01-20 14:25:52 UTC, updated_at: 2011-01-20 14:25:52 UTC, name: "aa", custom: nil>
ruby-1.9.2-p0 :034 > s.timers.count
=> 2
ruby-1.9.2-p0 :035 > s.timers
=> [#<Pomodoro _id: 4d384601c1037b05f3000006, created_at: 2011-01-20 14:26:09 UTC, updated_at: 2011-01-20 14:26:09 UTC, duration: 1500>, #<Pomodoro _id: 4d38461fc1037b05f3000007, created_at: 2011-01-20 14:26:39 UTC, updated_at: 2011-01-20 14:26:39 UTC, duration: 1500>]
ruby-1.9.2-p0 :036 > s.pomodoros
=> []
ruby-1.9.2-p0 :037 > s.pomodoros.where( :created_at.gt => 1.day.ago )
=> #<Mongoid::Criteria
selector: {:created_at=>{"$gt"=>2011-01-19 14:28:26 UTC}},
options: {},
count: 0,
matching: []>
ruby-1.9.2-p0 :038 > s.timers.where( :created_at.gt => 1.day.ago )
=> #<Mongoid::Criteria
selector: {:created_at=>{"$gt"=>2011-01-19 14:28:39 UTC}},
options: {},
count: 2,
matching: [#<Pomodoro _id: 4d384601c1037b05f3000006, created_at: 2011-01-20 14:26:09 UTC, updated_at: 2011-01-20 14:26:09 UTC, duration: 1500>, #<Pomodoro _id: 4d38461fc1037b05f3000007, created_at: 2011-01-20 14:26:39 UTC, updated_at: 2011-01-20 14:26:39 UTC, duration: 1500>]>
# What I expect is that I can access Pomodoro's belonging to a Session via either Session#timers or Session#pomodoros. What am I doing wrong?
class Pomodoro < Timer
set_callback(:validation, :before) do
self.duration = 25*60
end
end
class Session
include Mongoid::Document
include Mongoid::Timestamps
field :name
field :custom
field :created_at, type: DateTime
field :updated_at, type: DateTime
validates_uniqueness_of :name
validates_uniqueness_of :custom, :if => lambda{|s| s.custom }
embeds_many :timers
embeds_many :short_breaks
embeds_many :long_breaks
embeds_many :pomodoros
# tons of stuff omitted for brevity
end
class Timer
include Mongoid::Document
include Mongoid::Timestamps
field :duration, type: Integer
field :created_at, type: DateTime
embedded_in :session, inverse_of: :timers
validates_presence_of :duration
# tons of stuff omitted for brevity
end
@windoze
Copy link

windoze commented Mar 23, 2011

to_a may help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment