# Models not tied to DB

**URL:** <https://www.ruby-forum.com/t/models-not-tied-to-db/86369>\
**Category:** Rails\
**Created:** [February 21, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369 "2007-02-21T19:38:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 21, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/1 "2007-02-21T19:38:58Z")

</div>

Is there a way I can have a model that’s basically what would be used to  
simply encapsulate some data?

Here’s my situation. I have a search form that contains name fields and  
two date fields outputted using date\_select. I created a Search model  
but don’t have it tied to ActiveRecord. I’d like to do something like  
the following:

Search Controller  
…  
@search = MySearch.new(@params[:search])  
…

Search View  
…  
date\_select “search”, “from\_date”, :class =\> “text”, :start\_year =\>  
2007, :order =\> [:month, :day, :year]  
…

Problem is, since it isn’t tied to ActiveRecord so it’s not associated  
with the database, I’m not sure how I’d go about creating the model to  
work properly with the html helpers. I tried creating some local  
variables using write\_attribute but that’s part of ActiveRecord so it  
won’t work.

Any ideas?

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 21, 2007, 9:16pm UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/2 "2007-02-21T21:16:43Z")

</div>

Maybe I should generalize my question a little more. How should I  
handle a date\_select that isn’t tied directly to my DB model? I just  
need the date select values for handling the find conditions. But  
without a model, I can’t seem to set a default value on the date\_select.

The B. wrote:

> Is there a way I can have a model that’s basically what would be used to  
> simply encapsulate some data?
> 
> Here’s my situation. I have a search form that contains name fields and  
> two date fields outputted using date\_select. I created a Search model  
> but don’t have it tied to ActiveRecord. I’d like to do something like  
> the following:
> 
> Search Controller  
> …  
> @search = MySearch.new(@params[:search])  
> …
> 
> Search View  
> …  
> date\_select “search”, “from\_date”, :class =\> “text”, :start\_year =\>  
> 2007, :order =\> [:month, :day, :year]  
> …
> 
> Problem is, since it isn’t tied to ActiveRecord so it’s not associated  
> with the database, I’m not sure how I’d go about creating the model to  
> work properly with the html helpers. I tried creating some local  
> variables using write\_attribute but that’s part of ActiveRecord so it  
> won’t work.
> 
> Any ideas?

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 21, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/3 "2007-02-21T21:31:02Z")

</div>

The B. wrote:

> Is there a way I can have a model that’s basically what would be used to  
> simply encapsulate some data?
> 
> Here’s my situation. I have a search form that contains name fields and  
> two date fields outputted using date\_select. I created a Search model  
> but don’t have it tied to ActiveRecord. I’d like to do something like  
> the following:
> 
> Search Controller  
> …  
> @search = MySearch.new(@params[:search])  
> …
> 
> Search View  
> …  
> date\_select “search”, “from\_date”, :class =\> “text”, :start\_year =\>  
> 2007, :order =\> [:month, :day, :year]  
> …
> 
> Problem is, since it isn’t tied to ActiveRecord so it’s not associated  
> with the database, I’m not sure how I’d go about creating the model to  
> work properly with the html helpers. I tried creating some local  
> variables using write\_attribute but that’s part of ActiveRecord so it  
> won’t work.
> 
> Any ideas?

Since ruby is a duck-typed language, as long as your Search model quacks  
like ActiveRecord, than everything should be fine. And I believe the  
form helpers simply call the attribute as a method to get the value.

So:

\<%= text\_field ‘foo’, ‘bar’ %\>

Will get its value from:

@foo.bar

Knowing that, we can define a class with attr\_accessor’s for any  
instance variables we want to make from fields out of.

I just tried this simple experiment. Seems to work for me.

#model  
class Foo  
attr\_accessor :bar  
def initialize  
@bar = ‘testing’  
end  
end

#controller  
def foo  
@foo = Foo.new  
end

#view  
\<%= text\_field ‘foo’, ‘bar’ %\>  
yields:

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 21, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/4 "2007-02-21T22:00:02Z")

</div>

Hey thanks, I hadn’t run across the attr\_accessor yet. It’s almost  
working now. Is creating an object via “search =  
Search.new(@params[:search])” an ActiveRecord thing? I’m getting a  
“wrong number of arguments (1 for 0)” on that call. If so, I’m guessing  
I’ll just have to parse/set the attributes in the model manually.

Alex W. wrote:

> Since ruby is a duck-typed language, as long as your Search model quacks  
> like ActiveRecord, than everything should be fine. And I believe the  
> form helpers simply call the attribute as a method to get the value.
> 
> So:
> 
> \<%= text\_field ‘foo’, ‘bar’ %\>
> 
> Will get its value from:
> 
> @foo.bar
> 
> Knowing that, we can define a class with attr\_accessor’s for any  
> instance variables we want to make from fields out of.
> 
> I just tried this simple experiment. Seems to work for me.
> 
> #model  
> class Foo  
> attr\_accessor :bar  
> def initialize  
> @bar = ‘testing’  
> end  
> end
> 
> #controller  
> def foo  
> @foo = Foo.new  
> end
> 
> #view  
> \<%= text\_field ‘foo’, ‘bar’ %\>  
> yields:

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 22, 2007, 12:18am UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/5 "2007-02-22T00:18:40Z")

</div>

On 2/22/07, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> form helpers simply call the attribute as a method to get the value.  
> Tha my not be entirely true. It depends on what you want out of it.  
> It’s  
> very difficult for example to use validation if you don’t use AR.  
> There are a couple of plugins you may find useful

[http://www.agilewebdevelopment.com/plugins/active\_form](http://www.agilewebdevelopment.com/plugins/active_form)

[http://www.agilewebdevelopment.com/plugins/activerecord\_base\_without\_table](http://www.agilewebdevelopment.com/plugins/activerecord_base_without_table)

Hope that helps

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 21, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/6 "2007-02-21T23:35:46Z")

</div>

The B. wrote:

> Hey thanks, I hadn’t run across the attr\_accessor yet. It’s almost  
> working now. Is creating an object via “search =  
> Search.new(@params[:search])” an ActiveRecord thing? I’m getting a  
> “wrong number of arguments (1 for 0)” on that call. If so, I’m guessing  
> I’ll just have to parse/set the attributes in the model manually.

Remeber that “params[:search]” is a hash itself, so you can handle this  
in your initialize method of your model.

class Search  
FIELDS = [  
:list,  
:of\_attributes,  
:you\_want,  
:as\_insatnce\_variables  
]

attr\_accessor \*FIELDS

def initialize(values = {})  
values.each do |name, value|  
raise “Search does not have this field” unless  
FIELDS.include?(name)  
instance\_variable\_set “@#{name}”, value  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 22, 2007, 5:40am UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/7 "2007-02-22T05:40:32Z")

</div>

No… super super easy to do validations, etc without a db.

[http://www.bphogan.com/learn/pdf/tableless\_contact\_form.pdf](http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf)

As always, thanks to Rick O. for the original idea.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 22, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/8 "2007-02-22T15:19:28Z")

</div>

Alex W. wrote:

> The B. wrote:
> 
> > Hey thanks, I hadn’t run across the attr\_accessor yet. It’s almost  
> > working now. Is creating an object via “search =  
> > Search.new(@params[:search])” an ActiveRecord thing? I’m getting a  
> > “wrong number of arguments (1 for 0)” on that call. If so, I’m guessing  
> > I’ll just have to parse/set the attributes in the model manually.
> 
> Remeber that “params[:search]” is a hash itself, so you can handle this  
> in your initialize method of your model.
> 
> class Search  
> FIELDS = [  
> :list,  
> :of\_attributes,  
> :you\_want,  
> :as\_insatnce\_variables  
> ]
> 
> attr\_accessor \*FIELDS
> 
> def initialize(values = {})  
> values.each do |name, value|  
> raise “Search does not have this field” unless  
> FIELDS.include?(name)  
> instance\_variable\_set “@#{name}”, value  
> end  
> end  
> end

Excellent information by everyone who responded to my question. I’ll  
give this a try (and maybe try out the activerecord base plugin that was  
mentioned).

Thanks!

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 22, 2007, 6:19am UTC](https://www.ruby-forum.com/t/models-not-tied-to-db/86369/9 "2007-02-22T06:19:34Z")

</div>

On 2/22/07, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> No… super super easy to do validations, etc without a db.
> 
> [http://www.bphogan.com/learn/pdf/tableless\_contact\_form.pdf](http://www.bphogan.com/learn/pdf/tableless_contact_form.pdf)
> 
> As always, thanks to Rick O. for the original idea.

Yes this is nice… I had seen this a loooong time ago but I had no  
idea  
where I saw it. Thanx for the link

This is what the active\_record\_base\_without\_table plugin does. I have  
pasted the entire working code of the plugin here. The save method has  
not  
been implemented in this one.

module ActiveRecord  
class BaseWithoutTable \< Base  
self.abstract\_class = true

```
def create_or_update
  errors.empty?
end

class << self
  def columns()
    @columns ||= []
  end

  def column(name, sql_type = nil, default = nil, null = true)
    columns <<

```

ActiveRecord::ConnectionAdapters::Column.new(name.to\_s, default,  
sql\_type.to\_s, null)  
end  
end  
end  
end
