Sufficiently advanced technology...

Posted by Denny at 01:28 on Sat, 19 Oct 2019

Today's PR marks something very special for me; this is the first time that I've felt like a professional-level native/idiomatic Rails developer. This is exactly what I wanted to get from this project, exactly what I felt was missing from my Rails competence up until this point, so I was pleased about this PR all out of proportion to the few lines of new code added in it - those lines being the end result of my figuring out how to do something The Rails Way™: Template elements saving in update... using appropriate levels of Rails magic :-p

The magic in question is accepts_nested_attributes_for, a definitively Rails-ish bit of model adornment that allows you to build a form for, and then save, all of the child attributes (has_many) of the main model the form is for. As far as I'm aware, Perl doesn't have a similar trick that will wrap that up and tie a bow in it for you, so I was pretty pleased that in reading around the form building process for my 'templates (and templated resources) have elements' set-up, I found this feature, knocked up some demo code, confirmed it was literally exactly what I wanted right then, and got it in place (replacing a bunch of hand-rolled element-updating code) - all in one very happy-making coding session :D

One school of thought says that code is not finished when there's nothing left to add - it's finished when there's nothing left to remove :) - and this feature packs a lot of useful, common, and tedious to implement functionality into literally one extra line of code in my model (and a couple of extra lines in the form) - so it's pleasingly minimal. But I guess that's basically the definition of 'The Rails Way', right? Convention over code and all that. And like I say, this is the first time I've come out of a bit of development thinking/feeling "I solved that like a Rails developer", rather than just getting by on my general level of experience in web development in other languages. Today is a good day to be me!

Tags: templates elements magic rails activerecord models accepts_nested_attributes_for

« "But wait, there's more" Users and Authentication »

Comments

No comments found