Skip to content

Instantly share code, notes, and snippets.

@matpowel
Created July 23, 2011 10:04
Show Gist options
  • Save matpowel/1101257 to your computer and use it in GitHub Desktop.
Save matpowel/1101257 to your computer and use it in GitHub Desktop.
An ActiveRecord emulating Tableless model which works with Rails 3.1
class Tableless < ActiveRecord::Base
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()
@columns ||= [];
end
def self.columns_hash
h = {}
for c in self.columns
h[c.name] = c
end
return h
end
def self.column_defaults
Hash[self.columns.map{ |col|
[col.name, col.default]
}]
end
def self.descends_from_active_record?
return true
end
def persisted?
return false
end
# override the save method to prevent exceptions
def save( validate = true )
validate ? valid? : true
end
end
@clickworkorange
Copy link

@ragrawal, @matpowel: I've been trying to use this in a Rails 3.2 app and also get "No such table: 'tableless'" - what am I missing?

@matpowel
Copy link
Author

@mikrogroove as I said to @ragrawal, post a simplified version of your model code and I'll have a look. We're using that code as-is in 3.2 and it works perfectly.

@clickworkorange
Copy link

The base model:

class Tableless < ActiveRecord::Base

  def self.table_name
      self.name.tableize
  end

  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

  def self.descends_from_active_record?
    return true
  end

  def persisted?
    return false
  end

  # override the save method to prevent exceptions
  def save( opts = {} )
    options = { :validate => true }.merge(opts)
    options[:validate] ? valid? : true
  end
end

And an example use:

class Stuff < Tableless
  has_many :stuff_things
  has_many :things, :through => :stuff_things

  column :id, :integer
  column :name, :string
  column :value, :string

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

@clickworkorange
Copy link

@matpowel: Many thanks for taking the time to help! I'm suspecting that my problem has to do with not instantiating the model? My plan is to add to this "fake" table at boot, using values from a configuration file...

@clickworkorange
Copy link

I should add that with the example above I get "No such table: 'stuffs'" and not "tablelesses".

@matpowel
Copy link
Author

This all works for me.. note I removed your initialize method as it's superfluous, that's what the default one will do but without the unsafe eval.

class Thing < Tableless
  belongs_to :stuff_thing

  column :stuff_thing_id, :integer
  column :name, :string
end

class StuffThing < Tableless
  belongs_to :stuff
  has_many :things

  column :stuff_id, :integer
end

class Stuff < Tableless
  has_many :stuff_things
  has_many :things, :through => :stuff_things

  column :id, :integer
  column :name, :string
  column :value, :string
end

And then you can use them as normal, except that ARel won't chain the has many through very well, I don't know whether your stuff_things etc are actually real AR models, if they are it will work normally.

1.8.7 :064 >   s = Stuff.new( :id => 10, :name => "foo", :value => "bar" )
WARNING: Can't mass-assign protected attributes: id
 => #<Stuff id: nil, name: "foo", value: "bar"> 
1.8.7 :065 > s.stuff_things << StuffThing.new( :stuff => s )
 => [#<StuffThing stuff_id: nil>] 
1.8.7 :067 > s.stuff_things.length
 => 1
s.things << Thing.new( :name => "wild thing" )
 => [#<Thing stuff_thing_id: nil, name: "wild thing">]

@stevemartin
Copy link

Thanks for this gist, I made this demo with it: https://github.com/stevemartin/tableless_form

@huda-kh
Copy link

huda-kh commented Nov 4, 2024

Is anyone using this or similar in Rails 7?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment