riak-ripple / ripple

A rich Ruby modeling layer for Riak, Basho's distributed database
Other
619 stars 152 forks source link

Nil associations/embeds/links evaluate as true in some contexts #280

Closed IdahoEv closed 12 years ago

IdahoEv commented 12 years ago

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 .nil? works, but unless does not. ||= does not work, etc.


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 >

IdahoEv commented 12 years ago

version tested was current head: f28a4b940708

IdahoEv commented 12 years ago

I've bolded the places where the output is unexpected, in the console log.

myronmarston commented 12 years ago

This is a duplicate of #197.