If i embed or link another object, and the associated object is nil, the association method evaluates as 'true'. This means you have to explictly call .nil?() on an association to see if the object exists or not.
Since this is not at all how Ruby programmers expect nil to behave, it can result in some extremely confusing debugging situations ... if
class MyModel
include Ripple::Document
one :foo, :class_name => "EmbeddedModel"
property :name, String
end
class EmbeddedModel
include Ripple::EmbeddedDocument
If i embed or link another object, and the associated object is nil, the association method evaluates as 'true'. This means you have to explictly call .nil?() on an association to see if the object exists or not.
Since this is not at all how Ruby programmers expect nil to behave, it can result in some extremely confusing debugging situations ... if
class MyModel include Ripple::Document one :foo, :class_name => "EmbeddedModel"
property :name, String end
class EmbeddedModel include Ripple::EmbeddedDocument
property :awesomeness, Integer end
$ rails console Loading development environment (Rails 3.1.3) ruby-1.9.2-p290 :001 > mm = MyModel.new => <MyModel:[new] name=nil> ruby-1.9.2-p290 :002 > mm.foo => nil ruby-1.9.2-p290 :003 > print "no foo" if mm.foo.nil? no foo => nil ruby-1.9.2-p290 :004 > print "no foo" unless mm.foo => nil ruby-1.9.2-p290 :005 > mm.foo ||= EmbeddedModel.new(:awesomeness => 100) => nil ruby-1.9.2-p290 :006 > mm.foo => nil ruby-1.9.2-p290 :007 > mm.foo = EmbeddedModel.new(:awesomeness => 100) =>
ruby-1.9.2-p290 :008 > mm.foo
=>
ruby-1.9.2-p290 :009 > mm = MyModel.new
=> <MyModel:[new] name=nil>
ruby-1.9.2-p290 :010 > mm.foo.class
=> NilClass
ruby-1.9.2-p290 :011 > mm.foo == nil
=> true
ruby-1.9.2-p290 :012 > print "nil" unless nil
nil => nil
ruby-1.9.2-p290 :013 > print "it's nil" unless nil
it's nil => nil
ruby-1.9.2-p290 :014 > print "it's nil" unless mm.foo
=> nil
ruby-1.9.2-p290 :015 > mm.foo.nil? => true ruby-1.9.2-p290 :016 >