Need some meta-programming for a Rails app

I’m adding a layout to my app that I found at
Ruby on Rails - Layouts | Tutorialspoint.

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

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

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

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