read_attribute by alias name returns nil

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • 4.0.0 final
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      You can't read_attribute by alias name, but you can write_attribute by alias name...

      Example code (Stuff uses aliases, Thing does not):

      class Thing
        include Mongoid::Document
        field :bar, type: String
      end
      
      class Stuff
        include Mongoid::Document
        field :b, type: String, as: :bar
      end
      
      thing = Thing.create!(bar: 'foo')
      thing.bar
      thing[:bar]
      thing['bar']
      thing.read_attribute('bar')
      thing.read_attribute(:bar)
      thing.send('bar')
      
      stuff = Stuff.create!(bar: 'foo')
      stuff.bar
      stuff[:bar]                       # <---- doesn't work
      stuff['bar']                      # <---- doesn't work
      stuff.read_attribute('bar')       # <---- doesn't work
      stuff.read_attribute(:bar)        # <---- doesn't work
      stuff.send('bar')
      

      Example output (w/o aliases)

      irb(main):042:0> thing.bar
      => "foo"
      irb(main):043:0> thing[:bar]
      => "foo"
      irb(main):044:0> thing['bar']
      => "foo"
      irb(main):045:0> thing.read_attribute('bar')
      => "foo"
      irb(main):046:0> thing.read_attribute(:bar)
      => "foo"
      irb(main):047:0> thing.send('bar')
      => "foo"
      

      Example output (w aliases): note the nils

      irb(main):049:0> stuff.bar
      => "foo"
      irb(main):050:0> stuff[:bar]
      => nil
      irb(main):051:0> stuff['bar']
      => nil
      irb(main):052:0> stuff.read_attribute('bar')
      => nil
      irb(main):053:0> stuff.read_attribute(:bar)
      => nil
      irb(main):054:0> stuff.send('bar')
      => "foo"
      irb(main):055:0> stuff.read_attribute(:b)
      => "foo"
      irb(main):056:0> stuff.read_attribute('b')
      => "foo"
      irb(main):057:0> stuff['b']
      => "foo"
      irb(main):058:0> stuff[:b]
      => "foo"
      

            Assignee:
            Unassigned
            Reporter:
            dianaclarke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: