小编Paw*_*rek的帖子

如何在Rails 5中创建ActiveRecord无表格模型?

我尝试在数据库中创建没有表的自动类型转换的新模型.我试图继承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
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

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), …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails activemodel ruby-on-rails-5

5
推荐指数
3
解决办法
5407
查看次数