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
@RobertDeRose
Copy link

Could you give a quick little example of a model using this? does it look like this?
clase SomeModel
extend Tableless

belongs_to :AnyOldActiveRecordModel
has_many :SomeOtherACModel

attr_accessor :some_column_1, :some_column_2

end

or do you have to use the column syntax like the old rails 2.3 way?

@ragrawal
Copy link

ragrawal commented Nov 3, 2011

Hi I am having trouble using this. It seems rails is trying to hit DB and is not able to find "tableless" table

Ritesh

@matpowel
Copy link
Author

matpowel commented Nov 8, 2011

Here's an example use:

class CreditCard < Tableless
belongs_to :payment

column :payment_id, :integer
column :first_name, :string
column :last_name, :string
column :number, :string
column :month, :date
column :year, :date
column :verification_value, :string
end

p = Payment.new( :users => 2, :price => 100.00, :credit_card => { :first_name => "Test", :last_name => "User", :month => 5, :year => 2013 } )
p.credit_card.valid? # add validations to make this useful
p.credit_card.verification_value = 123

etc.

What are you trying to do with your models?

@betamatt
Copy link

I believe #save takes a hash in 3.1 instead of a boolean.

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

@ragrawal
Copy link

I tried this and was able to get it working. However, something funny is happening. Let A is a tableless model and has many B. Also assume a is an instance of A. if I do a.b then I get an empty array. However if I do a.b.limit(100) then I get all the valid objects. Any idea what happening over here.

@matpowel
Copy link
Author

@ragrawal if you post your model code here I'll take a look at it

@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