Search code examples
ruby-on-railsrubynamespacesglobal-namespace

Ruby (/rails?) top level namespace


I'm in the following situation:

I'm building a site in rails. I have two classes with the name User. One is an ActiveRecord, the other is a custom class that I'd like to use for seeding my database. Question:

Can I refer to the ActiveRecord class named User from within my custom class named User?


Solution

  • Two classes with the same name will cause weird behavior, actually it will be only one class that will contain methods from both files, this can cause tons of issues.

    You should namespace your seeder class inside a module, for instance :

    module Seeder
      class User
      end
    end
    

    Then use Seeder::User when you need to use this class.