轨道3.1中的无表模型

Vas*_*ich 7 ruby-on-rails-3.1

看起来这种方法在rails 3.1中不再起作用了.那么,有人有一个有效的解决方案吗?

实际上,我已经找到了这个要点.它解决了railscast 解决方案的问题columns_hashcolumn_defaults错误,但是ActiveRecord::ConnectionNotEstablished当我尝试编写一些属性时,我总是得到错误.

有什么想法吗?

dan*_*igb 13

Rails 3.1中最简单的无表格模型是:

class Session
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :email, :password
  validates :email, :presence => true
  validates :password, :presence => true

  def initialize(attributes = {})
    if attributes
      attributes.each do |name, value|
        send("#{name}=", value)
      end
    end
  end

  def persisted?
    false
  end
end
Run Code Online (Sandbox Code Playgroud)

ActiveModel :: Validations是可选的(仅在使用验证时).此外,构造函数不是必需的(但非常需要).


jan*_*szm 9

对于Rails/ActiveRecord 5.0,您需要重新定义private def self.load_schema!以避免检查table_name.另外,请注意column方法(Type)的一点点破解.

这是Rails 5.0/ActiveRecord 5.0的Tableless模型的完整列表

class Tableless < ActiveRecord::Base

  def self.columns
    @columns ||= []
  end

  def self.column(name, sql_type = nil, default = nil, null = true)
    type = "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize.new
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, type, null, '')
  end

  def self.columns_hash
    @columns_hash ||= Hash[columns.map { |column| [column.name, column] }]
  end

  def self.column_names
    @column_names ||= columns.map { |column| column.name }
  end

  def self.column_defaults
    @column_defaults ||= columns.map { |column| [column.name, nil] }.inject({}) { |m, e| m[e[0]] = e[1]; m }
  end

  # Override the save method to prevent exceptions.
  def save(validate = true)
    validate ? valid? : true
  end

  private

  def self.load_schema!
    columns_hash.each do |name, column|
      self.define_attribute(
        name,
        column.sql_type_metadata,
        default: column.default,
        user_provided_default: false
      )
    end
  end

end
Run Code Online (Sandbox Code Playgroud)

  • 感谢您分享您的工作,我可以摆脱与rails 5不兼容的`activerecord-tableless` (2认同)

Jos*_*des 7

这种无表情的东西似乎越来越像一个黑客,但混合只是不一样的东西(不记得究竟什么现在不起作用,我已经在几个月前处理过它,因为升级回到它3.1打破它).3.1.0rc4版本使用'columns_hash'方法覆盖,3.1.0也需要'column_defaults'覆盖.所以这是一个通过我的项目测试的版本.

class Tableless < 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

  def self.columns_hash
    @columns_hash ||= Hash[columns.map { |column| [column.name, column] }]
  end

  def self.column_names
    @column_names ||= columns.map { |column| column.name }
  end

  def self.column_defaults
    @column_defaults ||= columns.map { |column| [column.name, nil] }.inject({}) { |m, e| m[e[0]] = e[1]; m }
  end

  # Override the save method to prevent exceptions.
  def save(validate = true)
    validate ? valid? : true
  end
end
Run Code Online (Sandbox Code Playgroud)

希望这对你有用,

- 何塞


Joh*_*ley 6

您应该创建自己的模型类,并混合使用所需的ActiveModel部分(例如,验证).来自Yehuda Katz的博客文章详细介绍.