# Need some meta-programming for a Rails app

**URL:** <https://www.ruby-forum.com/t/need-some-meta-programming-for-a-rails-app/184468>\
**Category:** Rails\
**Created:** [March 3, 2010, 3:04am UTC](https://www.ruby-forum.com/t/need-some-meta-programming-for-a-rails-app/184468 "2010-03-03T03:04:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 3, 2010, 3:04am UTC](https://www.ruby-forum.com/t/need-some-meta-programming-for-a-rails-app/184468/1 "2010-03-03T03:04:49Z")

</div>

I’m adding a layout to my app that I found at  
[Ruby on Rails - Layouts | Tutorialspoint](http://www.tutorialspoint.com/ruby-on-rails/rails-layouts.htm).

They have a piece that ultimately feeds data into a sidebar:  
class BookController \< ApplicationController  
layout ‘standard’  
def list  
@books = Book.find(:all)  
end

I’ve got HomeController and I want to feed the names models I got,  
e.g. Home, User, Vendor for now, more to come. So I want to generate  
that list automatically. Then I’ll eliminate any I don’t want or  
suppress them all for a Logon page, etc.

Any ideas?

Thanks in advance,  
Richard

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 3, 2010, 3:58pm UTC](https://www.ruby-forum.com/t/need-some-meta-programming-for-a-rails-app/184468/2 "2010-03-03T15:58:04Z")

</div>

OK, that was I bad idea because in creating the links I have to  
supply stuff specific to each model, so I’m just coding them by  
hand. I was just trying to be DRY.

On Mar 2, 9:03 pm, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 3, 2010, 8:54pm UTC](https://www.ruby-forum.com/t/need-some-meta-programming-for-a-rails-app/184468/3 "2010-03-03T20:54:17Z")

</div>

One way I might do this is to create a table-less model that serves as  
a union model. You’ll have to write a find\_by\_sql union query to  
manually bring your different other models together into one generic  
data model, and write your own finder method to encapsulate it. Use  
the find\_by\_sql to generate particular columns to help determine which  
model the record originally came from, so you can generate your links  
correctly.

The benefit of doing it this way is that you can generate your  
find\_by\_sql string on the fly with great specificity, but a really  
complex query can be done as one query to the database. If you have  
associations you need to bring in, this could get extremely  
complicated really quickly, though. Or else you bring in your foreign  
keys and make sure you plug any holes for the types that don’t support  
them.

The downside is that you lose ActiveRecord conditions, plus you have  
to write a complicated union yourself. I haven’t seen a union gem that  
does multi-model selects. And again, be careful if you try to use AR  
for associations.

There are other ways to do this DRYly, that may be better for your  
situation. This is probably overkill. Maybe you just add some utility  
methods to your individual models (perhaps through a mix-in to make it  
DRY) to make it easier to generate your links more cleanly. Then you  
plug the results into your individual finds into an array, and iterate  
over that in your view to build the table.

View helpers?

On Mar 3, 6:56 am, RichardOnRails

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [March 8, 2010, 6:24pm UTC](https://www.ruby-forum.com/t/need-some-meta-programming-for-a-rails-app/184468/4 "2010-03-08T18:24:01Z")

</div>

Hi sax,

Thanks for the ideas. I’ll have to study them to see what I can  
apply. Right now, I’m back after a week of illness and facing a two-  
week deadline on my current project, so it’ll take me a little time  
to get my arms around “Rails Meta-programming”.

Best wishes,  
Richard
