self.table_name在具有不同rails模式的模型中不起作用

时间:2018-03-30 06:49:29

标签: ruby-on-rails postgresql activerecord ruby-on-rails-5

以下是用于创建设置架构的迁移,并添加表创建roleclass 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

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

我的db config

self.table_name

我正在使用class SettingRole < ApplicationRecord self.table_name "settings.roles" end class SettingUser < ApplicationRecord self.table_name 'settings.users' end 将模型与数据库表

相关联
SettingRole

我查看了answer

但是当我尝试访问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- 时,它会向我显示错误

std::string b;
std::string p;
const std::string sep;

1 个答案:

答案 0 :(得分:4)

如上所述here,您需要将其设置为:

self.table_name = 'settings.roles'