gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:34:02 25 4
gpt4 key购买 nike

下面是创 build 置架构和添加表创建用户角色的迁移

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

我的数据库配置

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

我正在使用 self.table_name 将模型与数据库表相关联

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

我检查了这个answer

但是当我尝试访问 SettingRole 时,它显示以下错误

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-

最佳答案

如前所述here , 看起来你需要将其设置为:

self.table_name = 'settings.roles'

关于ruby-on-rails - self.table_name 在具有不同 rails 模式的模型中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49569782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com