Lookup tables and scaffolding

It occurs to me as a missing feature that there’s no way to indicate a
lookup
table relationship (as opposed to other sorts of foreign key
relationships)
in Rails and that the scaffolding generator could recognise this lookup
and
render a drop-down list automagically for the lookup table.

Say you have an Address and a State, and the State is a lookup table.
In
addresses you have state_id which points to one of the entries in
states. In
your models you represent this with the odd-sounding foreign key
relationship
calls:

class Address #…
belongs_to :state

And the even less meaningful:

class State #…
has_many :addresses

Obviously, the scaffolding cannot take any such relationship and produce
a
lookup dropdown, because it wouldn’t make sense in anything other than
these
sort of lookup table scenarios. So, perhaps there’s room for some
alternative calls that the scaffolding generator could understand (and
that
would make more sense to write):

class Address #…
includes_a :state

class State #…
is_part_of :addresses

I’m pretty new to RoR, so I wanted to fly this past the minds on this
list
before thinking about it too much more. Perhaps I’ve got a solution to
a
non-problem that I only think is a problem through my own ignorance. Or
is
this a worthwhile avenue to put some more effort into? What are your
thoughts?

This is probably something that we all thought when first getting into
RoR (which for me, wasn’t that long ago), but adding that sort of
thing yourself isn’t difficult at all. Scaffolding is something rather
cool that rails can do, but when it comes to real world applications,
scaffolding alone doesn’t even nearly get the job done. Most people
either don’t use scaffolding at all, or only use it to do the most
basic parts of their project. What I’m trying to say is, you can put
it off all you like, but sooner or later, you’re going to have to
start learning to program in ruby.

As for your suggestion of includes_a and is_part_of, I can’t see it
becoming a core rails feature, but building a plugin like that might
be an interesting project for someone.

-Nathan

On 5/18/06, Jason K. [email protected] wrote:

I’m pretty new to RoR, so I wanted to fly this past the minds on this list
before thinking about it too much more. Perhaps I’ve got a solution to a
non-problem that I only think is a problem through my own ignorance. Or is
this a worthwhile avenue to put some more effort into? What are your
thoughts?

Try the Scaffolding Extensions Plugin:
http://wiki.rubyonrails.com/rails/pages/Scaffolding+Extensions+Plugin

You should also look at the ajax_scaffold_generator project:

http://www.ajaxscaffold.com

It allows you to customize the scaffold to do lookup tables as well.

Exactly.

The point of the scaffolds isn’t to write an app for you or design forms
for you; hardly anyone really uses the scaffold-generated forms in any
recognizabke form beyond the first half hour of a model’s life.

Scaffolding is less about generating code that does something than it is
about relieving the tedium and niggling errors that would result if
developers had to create all of the dozens to hundreds of initial, bare
files that make up a Rails app by hand and stick them in all the right
places.

Also bear in mind that scaffolds get generated before you have a
chance to establish relationships in the model as you propose. All the
scaffold generator has to go on is foreign key ID fields and maybe a
peek at the schema, and there’s no way to tell whether widget_id is
going to be pointing to a table that contains 20 widgets that would make
for a nice select pulldown, or 20,000 widgets stocked in a supermarket,
which may well crash a browser if fed into a select field.

unknown wrote:

This is probably something that we all thought when first getting into
RoR (which for me, wasn’t that long ago), but adding that sort of
thing yourself isn’t difficult at all. Scaffolding is something rather
cool that rails can do, but when it comes to real world applications,
scaffolding alone doesn’t even nearly get the job done. Most people
either don’t use scaffolding at all, or only use it to do the most
basic parts of their project. What I’m trying to say is, you can put
it off all you like, but sooner or later, you’re going to have to
start learning to program in ruby.

As for your suggestion of includes_a and is_part_of, I can’t see it
becoming a core rails feature, but building a plugin like that might
be an interesting project for someone.

-Nathan