J Amiel wrote:
I’m not sure whether to be appalled or elated over this trickery.
A: Elated. Glad I could help answer that one for you
Oh and its not “trickery” its “magic”! Where you say, “tada!” at the
One of the biggest complaints I hear about the rails naysayers is the
inability to look at the model class (a la ActiveRecord) and be able to
determine what fields it contains (forcing you to have to look in the
database schema) when doing your development.
You can get the columns of the underlying table via @obj.columns
/@obj.class.columns; if that helps.
So, prior to Dr Nic’s Magic models, you would have TWO places to look
for model information - the model class def (to see relationships and
validation rules) and the database schema for everything else.
Now (in certain circumstances), EVERYTHING can be found in the schema.
Something neat (and scary) about that.
It’s only a pity that there isn’t MORE meta-data that can be found via
the current ActiveRecord API. It’s limited by a “lowest-common
denominator” approach ; for example, you cannot discover any foreign
At the very least, it’s a super-neat trick.
At the most, it’s yet ANOTHER way to DRY by using convention over
configuration. If you are already setting limits in the schema (not
null, length, etc), why repeat it in your model?
Oooh. Hadn’t thought of a validation of length… Nice one. I think
someone else mentioned length before, but I hadn’t thought of generating
a validation for it.
I don’t know if I can force myself to take my hand off the ‘railing’ and
say, “look ma, no models!”…but I am overjoyed at the option.
I think you’ll quickly need your models if you want to add methods and
specific associations. Plus with something like the annotate_models
plugin, you can generate a comment header to show you the schema
With the exception of the validations which you currently cannot turn
off (if the column is not-null, then you get a validates_presence_of)
all the other features are still optional as required by the developer
(if you don’t want an association generated… don’t call the method!)
It all works on the assumption that if you call a method on an object
then the object assumes you were looking for something and tries to
help. The logic of Magic Models is, If no method currently exists
“perhaps he wanted an association to another table?” If it can generate
an association for you, it will, and will return the result. Else it
will return the normal error for missing method.