Search code examples
ruby-on-railsmodelmoduleprefix

Rails table_name_prefix missing


I have got the following dir structure

models/foo/setting.rb
models/foo.rb

foo.rb content

 module Foo
  def self.table_name_prefix
    'foo_'
  end
 end

and setting.rb content

class Foo::Setting < ActiveRecord::Base
end

As soon as I am calling Foo::Setting.find… I am getting an error SQLException: no such table: settings which is indeed correct because the table is named foo_settings so rails seems to ignore the table prefix specified for the module Foo.

What can I do so that rails considers the prefix?


Solution

  • You've define a method inside a module (Foo). This doesn't magically define that method on a class nested in that module.

    I'd try something like

    class Foo < ActiveRecord::Base
      self.abstract_class = true
      self.table_name_prefix = 'foo_'
    end
    

    And then inherit from Foo

    class Foo::Setting < Foo
    ...
    end