{"id":828,"date":"2010-10-07T05:57:00","date_gmt":"2010-10-07T05:57:00","guid":{"rendered":"https:\/\/www.keenertech.com\/?p=828"},"modified":"2010-10-07T05:57:00","modified_gmt":"2010-10-07T05:57:00","slug":"recipe-default-ordering-in-rails","status":"publish","type":"post","link":"https:\/\/staging.keenertech.com\/?p=828","title":{"rendered":"Recipe: Default Ordering in Rails"},"content":{"rendered":"\n<p>Sometimes it&#8217;s convenient to ensure that all rows in a database table are always retrieved in a specified order for consistency. It turns out that this is trivial to do in Rails (both Rails 2.3.x and Rails 3.x).<\/p>\n\n\n\n<p>I had a SETTINGS table in the database that defined the settings that were available in order to customize a product. Anywhere that those settings were listed, I wanted them to be listed in alphabetical order.<\/p>\n\n\n\n<p>In standard Rails fashion, I had a Setting model that corresponded to the database table. As shown below, a default scope can be defined that will automatically be applied whenever&nbsp;<em>find<\/em>&nbsp;is called for the model (unless overridden by the caller).<\/p>\n\n\n\n<p><strong>Listing 1: The Setting Model (Rails 2.3.x)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   class Setting &lt; ActiveRecord::Base\n       default_scope :order =&gt; 'name'\n   end\n<\/pre>\n\n\n\n<p><strong>Listing 2: The Setting Model (Rails 3.x)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   class Setting &lt; ActiveRecord::Base\n       default_scope order(:name)\n   end\n<\/pre>\n\n\n\n<p>That&#8217;s it. One line of code in Rails 2.3.x or Rails 3.x, and my objective was achieved throughout my entire application.<\/p>\n\n\n\n<p>On another note, AREL can be used in Rails 3.x to chain conditions:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;default_scope where(:deleted_at =&gt; nil).order(:name)<\/p>\n\n\n\n<p>Should you need to override a default scope in Rails 3.x, here&#8217;s how:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;Setting.with_exclusive_scope.order(:created_at).all<\/p>\n\n\n\n<p>Default scopes can be useful and convenient, just don&#8217;t overuse them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it&#8217;s convenient to ensure that all rows in a database table are always retrieved in a specified<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[160],"class_list":["post-828","post","type-post","status-publish","format-standard","hentry","category-technology","tag-rails"],"_links":{"self":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts\/828","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=828"}],"version-history":[{"count":0,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=\/wp\/v2\/posts\/828\/revisions"}],"wp:attachment":[{"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/staging.keenertech.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}