ruby-on-railsfixtures

How are IDs for fixtures generated?


I'm using fixtures to hold all the test data that I have.

Each row in my DB has the same ID across several rake db:fixtures:loads.

That is nice, because I can create folders in the project, like "people/12345" to hold additional data (files) for the models.

I'm interested to know, how are these generated IDs kept constant at every fixtures:load?

I suppose that rails passes the fixture label to the hash function in order to obtain the ID, instead of storing it somewhere.


Solution

  • Here is the code that generates the IDs:

    # Returns a consistent, platform-independent identifier for +label+.
    # Identifiers are positive integers less than 2^32.
    def self.identify(label)
      Zlib.crc32(label.to_s) % MAX_ID
    end
    

    You might have guessed that label is the label used in the fixture, eg: for YAML fixture:

    matz:
      first_name: Yukihiro
      last_name: Matsumoto
    
    
    # ID would be:
    1520334085