Nested Attributes and how much fun it is to give a talk with a live demo
Wolfram Arnold — Wed, 07/22/2009 - 07:29
Tonight, I presented at the East Bay Ruby Meetup on invitation of Jon Seidel, the host, a talk about a very cool new feature in Rails 2.3.
This feature is about allowing you to create complex forms that involve the creation, updating, and deletion of data from multiple nested models. By nested models, we mean models that have some sort of has_one/has_many - belongs_to relationship with each other. The new Rails feature is a model directive named "accepts nested attributes_for" that lets you send the nested hash from a form post to the parent model and all child associations will be updated.
If the preceding paragraph sounds cryptic and abstract to you, I apologize, but the point of this blog post is not the repeat the presentation. Take a look at the slides on Slideshare and at the demo project on github. The video filmed thankfully by Paul Lundahl will be forthcoming shortly. So you're covered on content.
What I wanted to share is how engaging and fun it was to do a full-on live demo. I started with a bit of theory and description of the problem and then we worked through building a full application (full thanks for rspec scaffold) with the key bits illustrated succinctly and I believe understandably.
The audience was great and engaged. They were with me with every typo, every little success and every little speedbump. What I thought had been a Rails bug turned out not to be thanks to the watchful eyes and ideas of a participant.
The big lesson for me tonight was that the sandwiching of Abstract—Example—Abstract rocks.
(Thanks to Josh Susser for his comments at the end of my talk last week at the SF Ruby Meetup with Sarah Allen.)
The first abstract piece introduces the problem, but if it stays there, it just remains academic and we're tempted to ask "so what?" The example brings it into our reality, we see the problem as it would show up in a real life scenario. If things stay there, it'll be nifty but the larger scope isn't revealed. The final piece of the abstract ties things together in a broader context.
My broader context was why Web API's shouldn't be chatty, why there should be few well defined entry points, why exposing the backend data schema to REST controllers is a bad idea, why keeping the skinny controller—fat model paradigm helps with testing and maintainability, why working in Rails is fun and cool.
No fewer than three people found themselves inspired to contribute their book tips, which is awesome. I had heard of some of them but had not read any of them but now I sure will take a closer look:


