[ANN] Dr Nicâ?s Magic Models


#1

Welcome! Welcome! Welcome! Ladies and Gentlemen, today you shall be
thrilled and dazzled by wonders of magical mystery.

Dr Nicâ??s Magic Models will now be unveiled to all. Mystery and magic
that you will be able to perform at home.

Within your ActiveRecord models, never again will you need to write:

* validates_presence_of validations
* has_many and belongs_to statements
* has_many :through statements!!

And for the finale, you will be amazed and astounded as you watch
ActiveRecord models appear from nowhere. No class definition, just any
old database tables you have lying around the home is all youâ??ll need to
perform this trick!

No cover charge. No free steak knives. No heavy lifting involved.

Installation, DIY magical instructions, and a world of mystery awaits
you at:
http://magicmodels.rubyforge.org

Dr Nic: http://drnicwilliams.com


#2

Wow, that looks pretty damn awesome! Right now, adding all the
validates, habtm, hm, etc. usually takes me a bit more time than I’d
care to spend. Having it all be automatic would be cool. It looks like
your plugin also automatically handles belongs_to – how about has_one?

Joe


#3

Joe wrote:

Wow, that looks pretty damn awesome! Right now, adding all the
validates, habtm, hm, etc. usually takes me a bit more time than I’d
care to spend. Having it all be automatic would be cool. It looks like
your plugin also automatically handles belongs_to – how about has_one?

Joe

Oooh. I forgot to write up that one. Let’s make it a secret feature!

In the case of the example on the website, if you did:

person = Person.find_first
person.memberships
=> list of Memberships

person.membership
=> :first item of Memberships list

So you get both for free based on which method name you call.


#4

http://magicmodels.rubyforge.org

BTW, an extension will come out in future for the Magic Models to work
with the Composite Primary Keys solution.

Cheers
Nic


#5

This looks very interesting. I’ll try it after work today. I’m curious
to
see how it works with the Dry Scaffold announced recently.

Do you have a URL for that?

Some questions:

Are there any performance penalities associated with all this magic? Is
all
this introspection done once or with each request?

If any relationships are defined, does magic models respect them?

It uses lazy evaluation - if you send “groups” to a Person object, and
that method doesn’t exist already (ie. you haven’t already defined the
has_many :groups, :through => :memberships association), then it will
build that assocation for you - just as if you’d done it manually in
your class definition. Once. Then the method will be there next time.

The only additional performance overhead is the one-time collection of
some table and column meta-data via the connection object.

The class creation is similar - if you ask for a constant that doesn’t
exist, e.g. Person, then it will look through the table model for a
table that might match such a class name, e.g. people, and build the
class on top of it. Next time you ask for that class, its already built.
Again, its a one-time meta-data request to database.

Ruby is wonderful :slight_smile:


#6

Hi Dr. Nic!

This looks very interesting. I’ll try it after work today. I’m curious
to
see how it works with the Dry Scaffold announced recently.

Some questions:

Are there any performance penalities associated with all this magic? Is
all
this introspection done once or with each request?

If any relationships are defined, does magic models respect them?

Hammed


#7

DRY Scaffold: http://dry.rubyforge.org/

Good to know about the low overhead. Looking forward to trying it out.


#8

Hammed M. wrote:

DRY Scaffold: http://dry.rubyforge.org/

Good to know about the low overhead. Looking forward to trying it out.

I’ll have a play with the Dry Scaffold (which looks very similar to the
www.ajaxscaffold.com solution) and find out how it reflectively
determines the associations.


#9

Haha, I love magic tricks!

Very nice trick Dr. Nic! Very nice trick indeed.

I am very curious on how much of an cost impact this incurs compared to
the
vanilla AR, but truth be told, it can be made up elsewhere. A Benchmark
would be nice to see how much it does impact it though :slight_smile:

Definately will be keeping an eye on this project. Congrats.

-NSHB

On 8/7/06, Dr Nic removed_email_address@domain.invalid wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Kind regards,

Nathaniel B.
President & CEO
Inimit Innovations Inc. - http://inimit.com


#10

Nathaniel B. wrote:

Haha, I love magic tricks!

I used to know a couple good card tricks and even a routine using sponge
balls. But I wasn’t very diligent at practising :slight_smile:

Very nice trick Dr. Nic! Very nice trick indeed.

I am very curious on how much of an cost impact this incurs compared to
the
vanilla AR, but truth be told, it can be made up elsewhere. A Benchmark
would be nice to see how much it does impact it though :slight_smile:

I really don’t think there’s a significant performance impact - the
first time the association is requested it is created. From then on, its
regular schedule programming.

There’s more “How it works” for you here:
http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/#comments

Cheers
Nic


#11

Nic,
thats great, im gonna test it… does it care about maximum length too?
there’s so much in our models were we weren’t DRY, e.g. the presence,
maximum length, even format of a field (number, date, datetime etc)
is that already in your gem or can it be done for next release?

2006/8/7, Dr Nic removed_email_address@domain.invalid:

  • has_many and belongs_to statements
    you at:


Michael S. removed_email_address@domain.invalid

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium


#12

Dr Nic wrote:

Welcome! Welcome! Welcome! Ladies and Gentlemen, today you shall be
thrilled and dazzled by wonders of magical mystery.

Wow! This is an amazing achievement, having been writing models
exclusively
yesterday, most of it repeat typing validates_presence_of and has_many
:through, this is friggin’ awesome!!

Though I do fear that if Rails keeps innovating and improving at this
rate there’s gonna very little for us programmers to do!

Fantastic work Dr Nic (and I love the presentation, I’m a sucker for
magic tricks!)


#13

Chris wrote:

Dr Nic wrote:

Welcome! Welcome! Welcome! Ladies and Gentlemen, today you shall be
thrilled and dazzled by wonders of magical mystery.

Wow! This is an amazing achievement, having been writing models
exclusively
yesterday, most of it repeat typing validates_presence_of and has_many
:through, this is friggin’ awesome!!

Sorry I didn’t release it a day earlier… :frowning: I just couldn’t find time
to write the website for it. But I did fix a bug in it whilst writing
the tutorial/magic show. So that’s a bonus :slight_smile:

Though I do fear that if Rails keeps innovating and improving at this
rate there’s gonna very little for us programmers to do!

We read blogs and forums to keep up-to-date with all the time saving
tricks and tools that are available!

Fantastic work Dr Nic (and I love the presentation, I’m a sucker for
magic tricks!)

I’m loving the AdSense on this thread:

  • Must-See New Blender
  • Magic Bullet
  • Coin Trick
  • Learn David Blaine Magic

#14

Can I use it with existing schemas ?

Stuart


#15

Michael S. wrote:

Nic,
thats great, im gonna test it… does it care about maximum length too?
there’s so much in our models were we weren’t DRY, e.g. the presence,
maximum length, even format of a field (number, date, datetime etc)
is that already in your gem or can it be done for next release?

It has all the same support/limitations that ActiveRecord
Associations/Validations have now.

If your old model class looked like:

class Account < ActiveRecord::Base
has_many :charges
belongs_to :user
validates_presence_of :initial_balance
end

and that worked for you, then you can replace it with:

class Account < ActiveRecord::Base
end

If you had more complicated has_many/belongs_to associations, or if you
used additional validations, then you’ll need to add those manually as
usual.

“DRY” - I should have used that in the magic show somehow…

Cheers
Nic


#16

Stuart Fellowes wrote:

Can I use it with existing schemas ?

Yes.

I actually conceived the idea of auto-generating the ActiveRecord
classes first, of all the features in the Magic Models, because I was
faced with a legacy database with 50+ tables in it. I’d rather spend two
days writing something like the Magic Models gem than spend one day
knuckling down and doing it the manual way :slight_smile:

Magic Models don’t currently work with Composite Primary Keys but I’ll
extend MM to do so in the future. Subscribe to the blog
(http://drnicwilliams.com) for an announcement.

Cheers
Nic


#17

I’m not sure whether to be appalled or elated over this trickery.

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.

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.

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?

Huh.

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.


#18

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 :slight_smile:

Oh and its not “trickery” its “magic”! Where you say, “tada!” at the
end.

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
keys.

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
definition.

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.

Cheers
Nic


#19

On Aug 8, 2006, at 4:09 PM, J Amiel wrote:

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.

This is simple and handy:

annotate_models
http://plugins.radrails.org/directory/show/12

For visualization there’s this

Visualize Models
http://visualizemodels.rubyforge.org/

– fxn


#20

2006/8/8, Dr Nic removed_email_address@domain.invalid:

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.

thats exactly what i wanted to say. just DRY


Michael S. removed_email_address@domain.invalid

www.stellar-legends.de - Weltraum-Browsergame im Alpha-Stadium