# ActiveRecord and Database Views

**URL:** <https://www.ruby-forum.com/t/activerecord-and-database-views/59325>\
**Category:** Rails\
**Created:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325 "2006-05-03T18:57:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/1 "2006-05-03T18:57:18Z")

</div>

Hiall,

If I have say 10 tables that i would like to wrap up in 1 view to  
manipulate data inside these tables, do I then need 10 model.rb files  
for all 10 tables plus 1 for the view, or do I just need 1 model.rb  
file for the view ?

cheers  
Martin

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/2 "2006-05-03T18:57:19Z")

</div>

You would not use a view for the model as such.

A view is called from a Controller and can be called anything you want.  
The  
controller method would prepare all the model instances that you  
require,  
and provide them to either the default view or the view specified.

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/3 "2006-05-03T18:57:21Z")

</div>

Ok… Sorri for the misunderstanding

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/4 "2006-05-03T18:57:19Z")

</div>

Hi Liquid,

I’m not sure you got me right, excuses if I’m wrong. I didn’t mean  
rails views, but database views, as in “create view blabla as select \*  
from …”

cheers  
Martin

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/5 "2006-05-03T18:57:21Z")

</div>

On May 1, 2006, at 5:03 AM, Martin G. wrote:

> Hiall,
> 
> If I have say 10 tables that i would like to wrap up in 1 view to  
> manipulate data inside these tables, do I then need 10 model.rb files  
> for all 10 tables plus 1 for the view, or do I just need 1 model.rb  
> file for the view ?

Only do what you think is necessary. If you’re just doing some  
reads… you only need the view… assuming that is all the data that  
you’ll need.

-Robby

Robby R.  
Founder & Executive Director

PLANET ARGON, LLC  
Ruby on Rails Development, Consulting & Hosting

> **[Planet Argon](https://www.planetargon.com)**
>
> Planet Argon is a Ruby on Rails development agency specializing in maintenance and support for existing Rails apps. Based in Portland, OR, Planet Argon has been a member of the Rails community since 2004.

[www.robbyonrails.com](http://www.robbyonrails.com)

+1 503 445 2457  
+1 877 55 ARGON [toll free]  
+1 815 642 4968 [fax]

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/6 "2006-05-03T18:57:23Z")

</div>

Hi Robby,

If I understand correct this means that if I want the underlying  
tables to be updatable via the view, i need to tell ActiveRecord about  
the underlying tables i.e. give it model classes, and if I don’t need  
them updatable then only one model class for the view is sufficient?

As my tables need to be updatable via the view this wouldn’t leave me  
with less model.rb files I guess ☹ I was asking this question because  
I am a little bit frustrated about the fact that obviously rails needs  
one .rb file per model … which is really polluting my app/models  
subfolder and makes files hard to find. (I have about 200 files as I  
have that much tables … If there would be a way to tell rails to  
look for more than one model class per .rb file, I would be very  
interested in that!!! If I currently try to put more than one model  
class per .rb file, rails complains about missing file “modelname.rb”  
…

Any opinions?

Martin

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/7 "2006-05-03T18:57:37Z")

</div>

Hi Joe,

Correct me if I am slightly delusional 🙂 but I can’t really follow  
your idea. In which way would your suggestion prevent tasteless file  
soups? If className is a folder, then which files would you expect  
underneath it? I have a table “foos” leading to a class “Foo” … If I  
now have a folder “Foo” below apps/models, which files should I place  
there?

Maybe you meant a folder for each controllerName, which would help a  
little, but still would be too inflexible in general, imo. All I  
really want, is that Rails goes looking for model classes in every .rb  
file it finds below app/models, and if it finds 1+ classes per file,  
this shouldn’t matter. Specifically, it shouldn’t be necessary for  
model class Foo to reside in app/models/foo.rb, it should be possible  
for this class to reside in app/models/bar.rb as well.

Any opinions?

Martin

---

<div class="post-metadata">

**Author:** ![martin-ga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc348/32.png) [@martin-ga](https://www.ruby-forum.com/u/martin-ga)\
**Post date:** [May 3, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/activerecord-and-database-views/59325/8 "2006-05-03T18:57:23Z")

</div>

I thought that one could place model files into their class named  
folder, so  
for example:

/app/models/className/classNameForModel.rb

Maybe I’m mistaken or slightly delusional but likely both o.O And if it  
doesn’t work like the stated behavior above … then it ought to be 😉

Should help you in file soup, at least things would be tucked nicely  
away in  
their own baggies.

JY
