【发布时间】:2017-05-20 13:57:38
【问题描述】:
我尝试创建具有自动类型转换的新模型,而数据库中没有表。我试图从ActiveRecord::Base继承它抛出异常ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "people" does not exist
类实现:
class Person < ActiveRecord::Base
def self.columns
@columns ||= [];
end
def self.column(name, sql_type = nil, default = nil, null = true)
@columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
columns
column :from_email, :string
column :to_email, :string
column :article_id, :integer
column :message, :text
def initialize
end
end
堆栈跟踪:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "people" does not exist
LINE 8: WHERE a.attrelid = '"people"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
(SELECT c.collname FROM pg_collation c, pg_type t
WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation),
col_description(a.attrelid, a.attnum) AS comment
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"people"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from /activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:88:in `async_exec'
from /activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:88:in `block in query'
from /activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:589:in `block in log'
from /activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
from /activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:87:in `query'
from /activerecord-5.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:739:in `column_definitions'
from /activerecord-5.0.1/lib/active_record/connection_adapters/postgresql/schema_statements.rb:227:in `columns'
from /activerecord-5.0.1/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
from /activerecord-5.0.1/lib/active_record/connection_adapters/schema_cache.rb:62:in `columns_hash'
from /activerecord-5.0.1/lib/active_record/model_schema.rb:441:in `load_schema!'
from /activerecord-5.0.1/lib/active_record/attributes.rb:233:in `load_schema!'
from /activerecord-5.0.1/lib/active_record/attribute_decorators.rb:28:in `load_schema!'
from /activerecord-5.0.1/lib/active_record/model_schema.rb:436:in `load_schema'
from /activerecord-5.0.1/lib/active_record/model_schema.rb:349:in `attribute_types'
from /activerecord-5.0.1/lib/active_record/attribute_methods.rb:179:in `has_attribute?'
... 3 levels...
from /railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /project/rails/bin/rails:9:in `<top (required)>'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
编辑:
都没有
extend ActiveModel::Naming
也没有
include ActiveModel::Model
无法实现隐式类型转换。
【问题讨论】:
-
请解释你的理由。 ActiveRecord 专门用于数据库表。 Active Record 将类连接到关系数据库表,为应用程序建立几乎零配置的持久层。 由于您没有表,也没有持久化的意图,这显然不合适,因此请解释您的场景而不是你的错误。
-
场景是我想在一个地方设置孔系统,并希望在管理面板中轻松地对其进行更改。我正在使用 PostgreSQL 和 gem“rails-settings-cached”。 Gem 没问题,但是当我从视图获取数据到控制器时,参数只是字符串。我想进行类型转换,所以我认为 ActiveModel 将是最干的方式。你觉得怎么样?
-
那为什么不实际使用持久化模型呢?否则,您的应用程序在部署中重新启动的所有内容都将消失。除了我们使用预配置的单例类进行设置
-
检查这个tableless article。您似乎缺少使用
extend ActiveModel::Naming的to_model和persisted?方法
标签: ruby-on-rails ruby activerecord ruby-on-rails-5 activemodel