public
Last active

An ActiveRecord emulating Tableless model which works with Rails 3.1

  • Download Gist
tableless.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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

Put this in your app/models dir and then extend it to get a tableless model that still supports all the associations like has_many, belongs_to etc, validations and basically smells like a real model.

We use it a fair bit for things like filter models, giving us very clean view and controller code.

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?

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

Ritesh

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?

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

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.

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

@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?

@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.

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

@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...

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

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">]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.