# How to make "virtual ActiveRecord" be short?

**URL:** <https://www.ruby-forum.com/t/how-to-make-virtual-activerecord-be-short/107113>\
**Category:** Rails\
**Created:** [June 18, 2007, 10:08am UTC](https://www.ruby-forum.com/t/how-to-make-virtual-activerecord-be-short/107113 "2007-06-18T10:08:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [June 18, 2007, 10:08am UTC](https://www.ruby-forum.com/t/how-to-make-virtual-activerecord-be-short/107113/1 "2007-06-18T10:08:58Z")

</div>

use ActiveRecord::ConnectionAdapters::Column can make a “virtual  
ActiveRecord”,but how to make it short or have it other method?

class EmailForm \< 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

column :title, :string  
column :body, :string

validates\_presence\_of :title  
validates\_presence\_of :body

end

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [June 18, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/how-to-make-virtual-activerecord-be-short/107113/2 "2007-06-18T19:36:47Z")

</div>

I’ve got a mixin that approaches this from the other angle:

[http://djur.desperance.net/ruby/fake\_active\_record.rb](http://djur.desperance.net/ruby/fake_active_record.rb)

It implements enough of the AR interface to make most validations  
work; human\_attribute\_name is implemented so error\_messages\_for works.  
All you have to do is ‘include FakeActiveRecord’ and define the  
attributes with attr\_accessor.

---

<div class="post-metadata">

**Author:** ![gpygood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/67e7ee/32.png) [@gpygood](https://www.ruby-forum.com/u/gpygood)\
**Post date:** [June 19, 2007, 3:46am UTC](https://www.ruby-forum.com/t/how-to-make-virtual-activerecord-be-short/107113/3 "2007-06-19T03:46:58Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I’ve got a mixin that approaches this from the other angle:
> 
> [http://djur.desperance.net/ruby/fake\_active\_record.rb](http://djur.desperance.net/ruby/fake_active_record.rb)
> 
> It implements enough of the AR interface to make most validations  
> work; human\_attribute\_name is implemented so error\_messages\_for works.  
> All you have to do is ‘include FakeActiveRecord’ and define the  
> attributes with attr\_accessor.

thank you.the error\_message does not show:(“can not be blank”)

model:  
require “fake\_active\_record”  
class Fake \< ActiveRecord::Base  
include FakeActiveRecord  
attr\_accessor :title  
validates\_presence\_of :title,:message=\>“can not be blank”  
end

view:  
\<%=error\_messages\_for :fake%\>  
\<%form\_tag :action=\>“fake” do %\>  
\<%fields\_for :fake do |f|%\>  
\<%=f.text\_field :title%\>\<%=error\_message\_on(:fake,:title)%\>  
\<%end%\>  
\<%=submit\_tag%\>  
\<%end%\>

controller:

def fake  
#if Fake.new(params[:fake])  
#else  
render :action=\>“fake\_new”  
#end  
end

def fake\_new  
@fake = Fake.new  
end
