【问题标题】:No implicit conversion of nil into String. dbview_cti没有将 nil 隐式转换为字符串。 dbview_cti
【发布时间】:2016-02-23 17:56:56
【问题描述】:

我使用dbview_cti gem 来制作类表继承 (CTI)。我有两个类 Person(抽象类)和 Client(继承 Person)。

问题:

当我尝试 make rake db:migrate 时,控制台写出这个错误:

StandardError: An error has occurred, this and all later migrations canceled:

    no implicit conversion of nil into String ../postgresql/utils.rb:24:in `quote_ident'

模范人物

class Person < ActiveRecord::Base
  self.abstract_class = true
  cti_base_class
end

模型客户端

class Client < Person
  cti_derived_class
end

迁移创建人

class CreatePeople < ActiveRecord::Migration
  def self.up
    create_table :persons do |t|
      t.string :pesel, null: false
      t.string :first_name, null: false
      t.string :last_name, null: false
      t.string :email, null: false
      t.date :data_of_birth, null: false
      t.string :password_digest, null: false
      t.timestamps null: false
    end
  end

  def self.down
    drop_table :persons
  end
end

迁移创建客户端

class CreateClients < ActiveRecord::Migration
  def change
    create_table :clients do |t|
      t.references :person
      t.timestamps null: false
    end

    cti_create_view('Client')
  end
end

有关错误的更多详细信息:

>     == 20160223135814 CreateClients: migrating ====================================
>     -- create_table(:clients)
>        -> 0.0348s
>     -- cti_create_view("Client")
>     rake aborted!
>     StandardError: An error has occurred, this and all later migrations canceled:
>     
>     no implicit conversion of nil into String/home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql/utils.rb:24:in
> `quote_ident'
>     /home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql/utils.rb:24:in
> `quoted'
>     /home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql/quoting.rb:31:in
> `quote_table_name'
>     /home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:738:in `column_definitions'
>     /home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:186:in
> `columns'
>     /home/lukas/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/schema_cache.rb:43:in
> `columns'

知道哪里错了吗?

【问题讨论】:

  • 尝试从 Person 中删除 abstract_class,我猜它是试图从 cti_base_class 中获取 table_name 并将其标记为抽象使其为零。

标签: ruby-on-rails ruby inheritance class-table-inheritance


【解决方案1】:

您的Person 类被标记为抽象类,因此它的table_namenil。一般来说 abstract_class 已添加,因此 gem 可以定义自己的 ActiveRecord::Base 子类,然后可以在应用程序中继承这些子类,而不会导致 STI 逻辑进入。您可以将其设为非抽象或设置表名以进行继承:

class Person < ActiveRecord::Base
  self.abstract_class = true
  self.table_name = 'people'
  cti_base_class
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-08-25
    • 2019-03-11
    • 2023-04-09
    相关资源
    最近更新 更多