self.table_name not working in model with different rails schema
Asked Answered
E

1

5

Below is migration for creating a setting schema and add table create user and role

class CreateSettingSchema < ActiveRecord::Migration[5.0]
    def up
    execute 'CREATE SCHEMA settings'
    create_table "settings.users" do |t|
      t.string :name
      t.string :email
      t.string :phone
      t.string :address
      t.timestamps
    end

    create_table "settings.roles" do |t|
      t.string :name
            t.timestamps
    end
  end

  def down
    drop_table "settings.roles"
    drop_table "settings.users"
    execute 'DROP SCHEMA settings'
  end

end

my db config

default: &default
  adapter:  postgresql
  host:     localhost
  encoding: unicode
  pool:     5
  username: postgres
  password: postgres
  schema_search_path: settings,public
development:
  <<: *default
  database: enc_attr_development
  username: postgres
  password: postgres

I am using self.table_name to associate the model with a database table

class SettingRole < ApplicationRecord
    self.table_name "settings.roles"
end
class SettingUser < ApplicationRecord
      self.table_name 'settings.users'
end

I checked this answer

but when I try to access SettingRole it shows me below error

2.3.1 :005 > SettingRole
ArgumentError: wrong number of arguments (given 1, expected 0)
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.6/lib/active_record/model_schema.rb:217:in `table_name'
    from /home/uzaif/learn/enc_attr/app/models/setting_role.rb:2:in `<class:SettingRole>'
    from /home/uzaif/learn/enc_attr/app/models/setting_role.rb:1:in `<top (required)>'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:477:in `load'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:477:in `block in load_file'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:662:in `new_constants_in'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:476:in `load_file'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:375:in `block in require_or_load'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:37:in `block in load_interlock'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies/interlock.rb:12:in `block in loading'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/concurrency/share_lock.rb:150:in `exclusive'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies/interlock.rb:11:in `loading'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:37:in `load_interlock'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.6/lib/active_support/dependencies.rb:358:in `require_or_load'
    from /home/uzaif/.rvm/gems/ruby-2.3.1/gems/activesupport-
Eccles answered 30/3, 2018 at 6:49 Comment(2)
What version of rails you are on?Nollie
@JagdeepSingh rails '~> 5.0.6'Eccles
N
6

As mentioned here, It looks like you need to set it as:

self.table_name = 'settings.roles'
Nollie answered 30/3, 2018 at 7:6 Comment(1)
A genuine mistake :)Nollie

© 2022 - 2024 — McMap. All rights reserved.