{"id":1849,"date":"2009-02-23T22:53:41","date_gmt":"2009-02-23T22:53:41","guid":{"rendered":"http:\/\/justthesam.com\/2009\/02\/activerecord-much-maligned-but-actually-ok\/"},"modified":"2009-02-23T22:53:41","modified_gmt":"2009-02-23T22:53:41","slug":"activerecord-much-maligned-but-actually-ok","status":"publish","type":"post","link":"https:\/\/justthesam.com\/?p=1849","title":{"rendered":"ActiveRecord &#8211; much maligned, but actually OK"},"content":{"rendered":"<p>I&#39;ve been furiously porting my Ramaze CRUD helper from <a href=\"http:\/\/datamapper.org\/\" target=\"_blank\" title=\"DataMapper Ruby ORM\" rel=\"noopener\">DataMapper<\/a> to <a href=\"http:\/\/ar.rubyonrails.com\/\" target=\"_blank\" title=\"ActiveRecord Ruby ORM\" rel=\"noopener\">ActiveRecord<\/a> and I&#39;ve been quite surprised to find that it&#39;s only taken a couple of hours. I should hastily point out for non-programmer types, that CRUD stands for Create Read Update Delete &#8211; the four basic operations you can perform on a data entity.<\/p>\n<p><\/p>\n<div>My helper makes it super-easy to produce basic CRUD web pages for any data entity, with just a couple of lines of code in your helper &#8211; a bit like Rails scaffolds but without the scaffolding! Sounds like magic, I hear you yell! Well yes, but it&#39;s my magic that I wrote from scratch so it&#39;s not magic to me \ud83d\ude42<\/p>\n<div>It&#39;s been an easy port between ORMs mostly because the concepts and syntax map over with relatively little fuss, and actually 98% of my CRUD helper code is not concerned with the DB, but rather with running the web UI. So the few places that actually perform a DB operation needed a bit of tweaking to use the right syntax, but it&#39;s been pretty smooth sailing. Hopefully it will be easier with AR to neatly deal with many-to-many associations without having to explicitly fool around with the join table. We shall see, as that&#39;s what foxed me with DataMapper and caused me to switch.<\/div>\n<p><\/p>\n<div>One very important thing to mention is that when using ActiveRecord outside of Rails you have to know that it was originally written for the single-threaded Rails world. Hence it just maintains a very small connection pool (say 3) and those connections quickly run out in a threaded environment &#8211; Ramaze in my case. This leads to strange pauses of several seconds as your request handling thread waits for a connection to come free. They come free after about 5 seconds of inactivity, so you get one reasonably soon, but the multi-second pause is infuriating and very confusing. A neatly packaged solution is handily presented and well explained here:&#0160;<a href=\"http:\/\/coderrr.wordpress.com\/2009\/01\/16\/monkey-patching-activerecord-to-automatically-release-connections\/\" target=\"_blank\" rel=\"noopener\">http:\/\/coderrr.wordpress.com\/2009\/01\/16\/monkey-patching-activerecord-to-automatically-release-connections\/<\/a>. Note that there is a link at the bottom to the single-file patch that just sorts everything out for you, so if you can&#39;t be bothered to read the explanations, head straight for that, require it in your project and you&#39;re away.<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve been furiously porting my Ramaze CRUD helper from DataMapper to ActiveRecord and I&#39;ve been quite surprised to find that it&#39;s only taken a couple of hours. I should hastily point out for non-programmer types, that CRUD stands for Create Read Update Delete &#8211; the four basic operations you can perform on a data entity. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23,33],"tags":[],"class_list":["post-1849","post","type-post","status-publish","format-standard","hentry","category-programming","category-ruby","category-webtech"],"_links":{"self":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1849"}],"version-history":[{"count":0,"href":"https:\/\/justthesam.com\/index.php?rest_route=\/wp\/v2\/posts\/1849\/revisions"}],"wp:attachment":[{"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/justthesam.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}