Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1147

A field called `identifier' behaves oddly

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.1.8
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      If I name a field identifier, then Mongoid doesn't complain, and lets me set it, but it always returns nil.

      I see attr_reader :identifier in the Mongoid::Keys module. That's probably what's doing it.

      The same behaviour is also witnessed with the name metadata, and presumably others too.

      At the very least, Mongoid should not allow fields to be created with "reserved words" as names if they're not going to work properly. But it'd be even better if they did work properly

      require "spec_helper"
      
      describe "An attribute" do
        before do
          class Model
            include Mongoid::Document
      
            field :name,       type: String
            field :identifier, type: String
          end
        end
      
        let(:model) { Model.new }
      
        shared_examples "an attribute" do
          before do
            model.send("#{attribute}=", "value")
          end
      
          it "is readable" do
            model.send(attribute).should == "value"
          end
      
          it "is persistable" do
            model.save
      
            model_reloaded = Model.find(model.id)
            model_reloaded.send(attribute).should == "value"
          end
        end    
      
        context "not called `identifier'" do
          let(:attribute) { :name }
          it_behaves_like "an attribute"
        end
      
        context "called `identifier'" do
          let(:attribute) { :identifier }
          it_behaves_like "an attribute"
        end
      end
      

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

              Created:
              Updated:
              Resolved: