# Q: ActiveForm?

**URL:** <https://www.ruby-forum.com/t/q-activeform/94899>\
**Category:** Rails\
**Created:** [November 13, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/q-activeform/94899 "2005-11-13T15:56:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@sross](https://www.ruby-forum.com/u/sross)\
**Post date:** [November 13, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/q-activeform/94899/1 "2005-11-13T15:56:51Z")

</div>

This question pops in and out of the mailing list and I’m wondering  
whether  
others have solved it. Consider the case where the user is filling out a  
form but there is no underlying database record (say the contents of the  
form are emailed someplace then discarded).

It would be great to use:

\<%= text\_field(:my\_fine\_form, ‘last\_name’) %\>  
  
\<%= text\_field(:my\_fine\_form, ‘first\_name’) %\>

# etc.

Then have params[:my\_fine\_form] automagically populate some object just  
as  
it does the model. E.g.,

@my\_fine\_data = MyFineForm.find() # populates from  
params[:my\_fine\_form]  
redirect\_to :action =\> ‘entry\_form’ unless @my\_fine\_data.valid?

And even cooler, a form model:

def MyFineForm \< ActiveForm  
:validates\_presence\_of :last\_name  
:persists\_in\_session =\> true;

# and blah, blah, blah

end

I’d write this myself – I may even have to – but I wanted to make sure  
nobody else already has.

Thanks

---

<div class="post-metadata">

**Author:** ![sross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@sross](https://www.ruby-forum.com/u/sross)\
**Post date:** [November 13, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/q-activeform/94899/2 "2005-11-13T15:56:51Z")

</div>

Steve R. wrote:

> :validates\_presence\_of :last\_name  
> :persists\_in\_session =\> true;
> 
> # and blah, blah, blah
> 
> end
> 
> I’d write this myself – I may even have to – but I wanted to make sure  
> nobody else already has.

Well, my hacky way of doing this was just to leverage the entire  
ActiveRecord  
class, but use a memory based sqlite table that represents the fields in  
the  
form I want. In the model class itself, I set up the table structure to  
represent the fields I want filled out. There’s nothing stored in a  
permanent  
database. One could even override save and create to throw exceptions  
in case  
anybody does try to save anything to the db.

ActiveForm could derive from ActiveRecord::Base and use a memory backed  
sqlite  
database as a backend for ActiveRecord to inflect off of.

Here’s the code:

# There are no records that are stored in a database for the model, so

# build a memory based Sqlite database to build a schema to help build

# an ActiveRecord.

# 

# Because no objects instances of this model are stored in the

# database, conventional usage of an object of this model is to have

# the object’s id be the same as the output type’s id so linking them

# up is easy.

class Post \< ActiveRecord::Base

unless const\_defined? :SQLITE\_SETUP  
SQLITE\_SETUP = true  
self.establish\_connection(:adapter =\> ‘sqlite’, :database =\>  
‘:memory:’)  
conn = self.connection  
conn.create\_table(self.table\_name,  
:primary\_key =\> self.primary\_key) do |t|  
t.column :count, :integer  
end  
end

attr\_accessible :count  
validates\_presence\_of :count  
validates\_numericality\_of :count, :only\_integer =\> true  
end

Regards,  
Blair

---

<div class="post-metadata">

**Author:** ![sross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@sross](https://www.ruby-forum.com/u/sross)\
**Post date:** [November 13, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/q-activeform/94899/3 "2005-11-13T15:56:51Z")

</div>

Great solution. Just a couple notes:

1. Not everyone has sqlite. Notably, most Win32 installs don’t.  
Fortunately,  
that’s not where I’m developing or where I’m targeting, to I’m ok.

2. self.establish\_connection(:adapter =\> ‘sqlite’, :database =\>  
‘:memory:’)  
– should read –  
self.establish\_connection(:adapter =\> ‘sqlite’, :dbfile =\>  
‘:memory:’)

Steve

---

<div class="post-metadata">

**Author:** ![sross](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@sross](https://www.ruby-forum.com/u/sross)\
**Post date:** [November 13, 2005, 3:56pm UTC](https://www.ruby-forum.com/t/q-activeform/94899/4 "2005-11-13T15:56:52Z")

</div>

Steve R. wrote:

> Great solution. Just a couple notes:
> 
> 1. Not everyone has sqlite. Notably, most Win32 installs don’t. Fortunately,  
> that’s not where I’m developing or where I’m targeting, to I’m ok.

That’s good!

> 1. self.establish\_connection(:adapter =\> ‘sqlite’, :database =\> ‘:memory:’)  
> – should read –  
> self.establish\_connection(:adapter =\> ‘sqlite’, :dbfile =\> ‘:memory:’)

Actually not. In a recent RoR release, this was changed:

[http://dev.rubyonrails.com/changeset/2825](http://dev.rubyonrails.com/changeset/2825)

Regards,  
Blair
