Tubeless model in rails 3.1

It looks like this method no longer works in rails 3.1. So does anyone have a working solution?

Actually, I found this gist . It solves the problems with the columns_hash and column_defaults from the railscast solution, but I get an ActiveRecord::ConnectionNotEstablished error all the time when I try to write some attribute.

Any thoughts?

+7
source share
6 answers

You have to create your own model class and mix in those parts of ActiveModel (for example, checks) that you need. This blog post by Yehuda Katz contains details.

+6
source share

The simplest model without tables in Rails 3.1 is:

 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 

ActiveModel :: Validations is optional (only if validations are used). Also, a constructor is not required (but very desirable).

+13
source share

For Rails / ActiveRecord 5.0 you need to override private def self.load_schema! to avoid checking table_name . Also pay attention to a small hack to the column method (Type).

Here is the complete list for a tableless model for Rails 5.0 / ActiveRecord 5.0

 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 
+9
source share

This bottomless thing seems more and more hacked, but the mix is ​​just not the same (I don’t remember exactly what is not working now, I talked to him a few months ago, returned because updating to 3.1 broke it). Version 3.1.0rc4 worked with an override of the "columns_hash" method, 3.1.0 also requires an override of "column_defaults". So, here is the version that passes my design tests.

 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 

Hope this works for you

- Jose

+7
source share

For Rails 3.2 there is activerecord-tableless . This is the jewel for creating inactive ActiveRecord models, so it supports validations, associations, types.

When you use the recommended way to do this in Rails 3.x, there is no support for associations and types.

+1
source share

and for Rails 3.2, RUBY should be preferred to 1.9.3 to avoid incompatibilities.

0
source share

All Articles