<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>databasically // Kansas City Small Business IT &#38;&#38; Ruby on Rails Programming &#187; Ruby on Rails</title>
	<atom:link href="http://databasically.com/category/blog/programming/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://databasically.com</link>
	<description>Kansas City Small Business IT</description>
	<lastBuildDate>Fri, 20 Apr 2012 04:38:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Lowdown is now free and open-source</title>
		<link>http://databasically.com/2012/04/09/lowdown-is-now-free-and-open-source/</link>
		<comments>http://databasically.com/2012/04/09/lowdown-is-now-free-and-open-source/#comments</comments>
		<pubDate>Mon, 09 Apr 2012 19:16:03 +0000</pubDate>
		<dc:creator>Wes</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=657</guid>
		<description><![CDATA[Lowdown is now open-source]]></description>
			<content:encoded><![CDATA[<p>Lowdown was a Rails Rumble project in 2009, aiming to help business owners that want custom software work with developers and designers to get on the same page.</p>
<p>We continued to maintain Lowdown after Rails Rumble, but it&#8217;s time to let it go live its own life.</p>
<p>So, as of today: </p>
<ol>
<li>Lowdown is now 100% free with unlimited projects and unlimited collaborators. All current and paid subscribers have been converted to the free plan. </li>
<li>Lowdown is now Open Source Software. For more information please visit:  <a href="http://github.com/databasically/lowdown">http://github.com/databasically/lowdown</a></li>
<li>Support for the free hosted version has moved to Github issues. Please see #2 for more information. </li>
<li>We&#8217;ve migrated Lowdown to Heroku. All current projects will be intact, although there may be minor (if any) disruption in service as DNS propagates. Thanks for your patience.</li>
</ol>
<p>Thanks for trying/using Lowdown! If you&#8217;re interested in participating in the OS version, please contact us: <a href="http://twitter.com/databasically">@databasically</a></p>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2012/04/09/lowdown-is-now-free-and-open-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bundler 1.1 vs 1.0 install speedup</title>
		<link>http://databasically.com/2012/03/08/bundler-1-1-vs-1-0-install-speedup/</link>
		<comments>http://databasically.com/2012/03/08/bundler-1-1-vs-1-0-install-speedup/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 20:11:57 +0000</pubDate>
		<dc:creator>Wes</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=646</guid>
		<description><![CDATA[Bundler 1.1 went official yesterday.  One of the big improvements is speed.  Previous versions of bundler would need to download all of the rubygems information in order to build a dependency tree. Whenever you see Fetching source index for http://rubygems.org/ when you do a bundle install, that&#8217;s bundler downloading the file with all of the gems&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://gembundler.com/">Bundler</a> 1.1 went official yesterday.  One of the big improvements is speed.  Previous versions of bundler would need to download all of the rubygems information in order to build a dependency tree.</p>
<p>Whenever you see <code>Fetching source index for http://rubygems.org/</code> when you do a <code>bundle install</code>, that&#8217;s bundler downloading the file with all of the gems&#8217; information.</p>
<p>Now, bundler 1.1 is smarter about this.  It can ask rubygems.org directly to give it the dependencies for a list of gems (see this post for a better explanation of the actual <a href="http://patshaughnessy.net/2011/10/14/why-bundler-1-1-will-be-much-faster">rubygems bundler API calls</a>) and use that to start downloading the gems.</p>
<p>Here are my results, for a project with a brand new empty gemset:</p>
<pre name="code" class="ruby">
$ gem install bundler -v=1.0.18 &#038;&#038; time bundle install
Installing ...
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

real	48.174s
user	38.411s
sys	19.535s

$ gem install bundler -v=1.1.0 &#038;&#038; time bundle install
Fetching gem metadata from http://rubygems.org/.......
Fetching gem metadata from http://rubygems.org/..

Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

real	22.015s
user	28.377s
sys	18.442s
</pre>
<p>Speedy!</p>
<p>Check out the <code>bundle install --verbose</code> option if you want more insight into what bundler is doing, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2012/03/08/bundler-1-1-vs-1-0-install-speedup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Kansas City Ruby:  What&#8217;s New in Ruby &#8211; February 2012</title>
		<link>http://databasically.com/2012/02/22/kansas-city-ruby-whats-new-in-ruby-february-2012/</link>
		<comments>http://databasically.com/2012/02/22/kansas-city-ruby-whats-new-in-ruby-february-2012/#comments</comments>
		<pubDate>Wed, 22 Feb 2012 01:36:55 +0000</pubDate>
		<dc:creator>Wes</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Kansas City]]></category>
		<category><![CDATA[Kansas City Ruby User Group]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=639</guid>
		<description><![CDATA[At the beginning of each Kansas City Ruby meeting, I do a quick presentation on some new neat things from the last month in Ruby news. &#160; What&#8217;s New In Ruby February 2012 Ruby on Rails 3.2 released http://weblog.rubyonrails.org/ Faster development mode End of Ruby 1.8.7 puts Person.active.limit(5).explain Automatically shows when &#62; half a second [...]]]></description>
			<content:encoded><![CDATA[<p>At the beginning of each Kansas City Ruby meeting, I do a quick presentation on some new neat things from the last month in Ruby news.</p>
<p><iframe src="https://docs.google.com/present/embed?id=dgw9ksj_1cbtfshfp&amp;autoStart=true&amp;loop=true" frameborder="0" width="410" height="342"></iframe></p>
<p>&nbsp;</p>
<h2>What&#8217;s New In Ruby</h2>
<h3>February 2012</h3>
<h4>Ruby on Rails 3.2 released</h4>
<p><a href="http://weblog.rubyonrails.org/">http://weblog.rubyonrails.org/</a></p>
<ul>
<li>Faster development mode</li>
<li>End of Ruby 1.8.7</li>
<li>
<pre>puts Person.active.limit(5).explain</pre>
</li>
<li>Automatically shows when &gt; half a second</li>
<li>TaggedLogger</li>
</ul>
<h4></h4>
<h4>ActiveRecord Store</h4>
<pre>class User &lt; ActiveRecord::Base
  store :settings, accessors: [ :color, :homepage ]
end</pre>
<pre>u = User.new(color: 'black', homepage: '37signals.com')
u.color # Accessor stored attribute</pre>
<pre>  # Any attribute, even if not specified with an accessor
u.settings[:country] = 'Denmark'</pre>
<h3>RubyMine 4.0 released</h3>
<p><a href="http://blog.jetbrains.com/ruby/2012/02/rubymine-4-is-here-to-make-you-feel-the-productivity/">http://blog.jetbrains.com/ruby/2012/02/rubymine-4-is-here-to-make-you-feel-the-productivity/</a></p>
<p>RubyMine is a popular Ruby and Rails IDE by JetBrains (the folks behind IntelliJ IDEA).</p>
<p>A focus has been put on improving its performance and UI, but it now also supports all of Rails 3.2 features, including CoffeeScript compilation right from the IDE.</p>
<h3>Spree 1.0 Released</h3>
<p><a href="http://spreecommerce.com/">http://spreecommerce.com/</a></p>
<p>Spree is almost certainly the most popular, fully featured Rails-based e-commerce system and its creators are proud to announce the release of version 1.0.0.</p>
<h3>Strano</h3>
<p><a href="https://github.com/joelmoss/strano">https://github.com/joelmoss/strano</a><br />
The Github backed Capistrano deployment management UI.</p>
<h3>Guard::RSpectacle</h3>
<p><a href="https://github.com/netzpirat/guard-rspectacle">https://github.com/netzpirat/guard-rspectacle</a><br />
Guard::RSpectacle automatically tests your application with RSpec when files are modified.</p>
<h3>Lightning Talks</h3>
<ul>
<li>Samuel Mullen: using search provider shortcuts in Google Chrome, <a href="http://samuelmullen.com/2012/02/power-up-search-in-google-chrome/">blog post</a></li>
<li>Jaime Bellmyer: searching with <a href="http://leap2.com/">leap2</a></li>
<li>Advanced REST Client: help view API responses with Chrome app <a href="https://chrome.google.com/webstore/detail/hgmloofddffdnphfgcellkdfbfbjeloo">link</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2012/02/22/kansas-city-ruby-whats-new-in-ruby-february-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Configuration for mailjet email delivery with Ruby on Rails</title>
		<link>http://databasically.com/2011/06/07/configuration-for-mailjet-email-delivery-with-ruby-on-rails/</link>
		<comments>http://databasically.com/2011/06/07/configuration-for-mailjet-email-delivery-with-ruby-on-rails/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 10:00:03 +0000</pubDate>
		<dc:creator>Wes</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=583</guid>
		<description><![CDATA[Setting up mailjet.com to deliver your mail via Ruby on Rails? Here&#8217;s how, because their Getting Started is nothing but placeholder headers right now. Create an account Go to https://www.mailjet.com and create an account. Note: the word &#8220;Faculatative&#8221; means &#8220;optional&#8221; on the signup form. Add your sending address Add a sending address (Accounts > Sender [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up mailjet.com to deliver your mail via Ruby on Rails?  Here&#8217;s how, because their <a href="https://www.mailjet.com/docs/getting_started">Getting Started</a> is nothing but placeholder headers right now.</p>
<div class="thumbnail"><a href="https://skitch.com/wesg/fdkc8/mailjet-real-time-emailing-mailjet.com"><img src="https://img.skitch.com/20110607-bmbfb3f4pjrcawsya4yab9dcic.preview.jpg" alt="Mailjet : Real-time Emailing - mailjet.com" /></a></div>
</p>
<p><span id="more-583"></span><br />
<h2>Create an account</h2>
<p>Go to <a href="https://www.mailjet.com">https://www.mailjet.com</a> and create an account.</p>
<p>Note: the word &#8220;Faculatative&#8221; means &#8220;optional&#8221; on the signup form.</p>
<h2>Add your sending address</h2>
<ul>
<li>Add a sending address (Accounts > <a href="https://www.mailjet.com/account/sender">Sender Addresses</a>)</li>
<li>Update your Rails ActionMailer configuration
<ul>
<li>Find the settings you need at: <a href="https://www.mailjet.com/account/setup">https://www.mailjet.com/account/setup</a></p>
<pre>
    SMTP server:
    in.mailjet.com

    Username (API Key):
    12345678901234567890

    Password (Secret Key):
    99999999999999999999999999999999

    Port:
    25 or 587 (some providers block port 25)

    Use TLS:
    yes
  </pre>
</li>
<li>Set these settings in config/application.rb (or config/environments/production.rb):
<pre name="code" class="ruby">

    config.action_mailer.raise_delivery_errors = false
    config.action_mailer.perform_deliveries = true

    config.action_mailer.smtp_settings = {
      :address => "in.mailjet.com",
      :enable_starttls_auto => true,
      :port => 587,
      :authentication => 'plain',
      :user_name => "12345678901234567890",
      :password => "99999999999999999999999999999999"
    }
</pre>
</li>
</ul>
</li>
</ul>
<h2>Setup domain authentication (SPF and/or DomainKeys/DKIM)</h2>
<h3>SPF</h3>
<p>
  <img src="https://img.skitch.com/20110607-nmn91j4ukdp8yna3pup3qu1sb8.png" alt="My Account - mailjet.com" /><br />
  Click <b>Domain DNS and SMTP setup</b>.<br />
  Then, under <b>Domain Authentication</b>, click your domain.
</p>
<p>Setting up SPF requires adding a DNS entry. Mailjet will look at your existing one and give you the one to replace it with.<br />
  It&#8217;ll look something like: <br /><code>domainname.com.	IN TXT "v=spf1 include:spf.mailjet.com mx ~all"</code><br />
  In my DNS manager, I created a TXT record, gave it a name of &#8220;domainname.com.&#8221; and set the value to the <br /><code>'v=spf1 include:spf.mailjet.com mx ~all'</code> portion (without the quotes).  The full example they give you is for the DNS zone syntax, which you may or may not have.</p>
<p>  If you need help configuring this, <a href="/contact-us">we would be happy to help you</a>.</p>
<h3>DKIM</h3>
<p>
  This is similar, a TXT record that has a key that identifies your server so mail can be sent.  Same deal.
</p>
<h2>Test</h2>
<p>Deploy your changes and kick off something that sends a mailer.  We have a Notifier model that has a <code>test_email()</code> method that I just called from the console to push a test through.  I just went to the reports and saw it there.</p>
<p>All in all, the service seems a little rough around the edges, mostly due to it being a French company that could use a little work on the translations and some serious love on the support documentations.  Hope this helps you get started with Mailjet configuration!</p>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2011/06/07/configuration-for-mailjet-email-delivery-with-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gateway timeout with nginx/passenger standalone</title>
		<link>http://databasically.com/2011/04/18/gateway-timeout-with-nginxpassenger-standalone/</link>
		<comments>http://databasically.com/2011/04/18/gateway-timeout-with-nginxpassenger-standalone/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 15:18:52 +0000</pubDate>
		<dc:creator>Wes</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=528</guid>
		<description><![CDATA[I needed to run Rails apps with both ruby 1.8.7 and 1.9.2 on the same server. Passenger Standalone to the rescue! Setting up my 1.9.2 app as a standalone server and setting it up as proxy to it worked great. Until we had to upload and process some files. Turns out, the gateway server would [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to run Rails apps with both ruby 1.8.7 and 1.9.2 on the same server.</p>
<p><a href="http://www.modrails.com/documentation/Users%20guide%20Standalone.html">Passenger Standalone</a> to the rescue!  Setting up my 1.9.2 app as a standalone server and setting it up as proxy to it worked great.</p>
<p><strong>Until</strong> we had to upload and process some files.  Turns out, the gateway server would timeout, even though the process was still processing on the app server.</p>
<p>The proxing nginx server would reply with &#8220;Gateway Timeout 504&#8243;.</p>
<p>The fix from the nginx documentation: <a href="http://wiki.nginx.org/HttpProxyModule#proxy_read_timeout">proxy_read_timeout</a></p>
<p>My proxy config after this:</p>
<pre name="code" class="ruby">
  location / {
    proxy_pass http://127.0.0.1:3010;
    proxy_read_timeout 240s;
  }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2011/04/18/gateway-timeout-with-nginxpassenger-standalone/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thebes, profiling spork, backup rubygem</title>
		<link>http://databasically.com/2011/03/14/thebes-profiling-spork-backup-rubygem/</link>
		<comments>http://databasically.com/2011/03/14/thebes-profiling-spork-backup-rubygem/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 15:41:34 +0000</pubDate>
		<dc:creator>Wes</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=506</guid>
		<description><![CDATA[Thebes, a new minimal sphinx gem for Rails Link Thebes is a wrapper around Sphinx, the search engine we use on most of our projects. Thebes differs from other solutions by staying as far away from your Rails code as possible. Instead of hiding the Sphinx configuration file behind a domain-specific language, this library assumes [...]]]></description>
			<content:encoded><![CDATA[<h1 id="thebes_a_new_minimal_sphinx_gem_for_rails">Thebes, a new minimal sphinx gem for Rails</h1>
<p><a href="www.getharvest.com/blog/2011/03/thebes-a-new-minimal-sphinx-gem-for-rails/" title="Thebes">Link</a></p>
<blockquote>
<p>Thebes is a wrapper around Sphinx, the search engine we use on most of our projects. Thebes differs from other solutions by staying as far away from your Rails code as possible. Instead of hiding the Sphinx configuration file behind a domain-specific language, this library assumes you will write Sphinx config files by hand. In Thebes, you edit an ERB template of your Sphinx configuration and populate it with variables at generation time. For developers needing the most flexible or fastest solution possible, this is a great way to work with Sphinx.</p>
</blockquote>
<p>We&#8217;ve been doing some interesting things with search and reporting that are going to require faster lookups than directly querying the database. From the article &#8220;the [Thinking Sphinx project] has a lot of complexity and ties to ActiveRecord 2.x code. Consequently, the porting of TS to Rails 3 isn’t turning out to be the smooth road we hoped for.  So, for Rails 3 projects, this looks like a good way to go if you&#8217;re willing to get your hands dirty and build some sphinx files yourself.</p>
<h1 id="profiling_spork_for_faster_start_up_time">Profiling Spork for faster start-up time</h1>
<p><a href="http://opinionated-programmer.com/2011/02/profiling-spork-for-faster-start-up-time/" title="Profiling Spork">Link</a></p>
<p>Spork allows you to preload Rails environment files into a process, then it forks that process and runs your tests against the new process.  In essence, your tests will start faster because they&#8217;re not loading everything.  You can specify files you want to be reloaded each time (for instance, model files).</p>
<p>The code:</p>
<p><script src="https://gist.github.com/869238.js"> </script></p>
<p>This prints out everything being loaded up, so you can move files that don&#8217;t change into the preload block for that extra bit of snappiness.</p>
<h1 id="backup_a_rubygem_for_database_and_file_backups">Backup, a rubygem for database and file backups</h1>
<p><a href="https://github.com/meskyanichi/backup" title="Backup rubygem">Link</a></p>
<blockquote>
<p>Backup is a RubyGem (for UNIX-like operating systems: Linux, Mac OSX) that allows you to configure and perform backups in a simple manner using an elegant Ruby DSL. It supports various databases (MySQL, PostgreSQL, MongoDB and Redis), it supports various storage locations (Amazon S3, Rackspace Cloud Files, Dropbox, any remote server through FTP, SFTP, SCP and RSync), it can archive files and folders, it can cycle backups, it can do incremental backups, it can compress backups, it can encrypt backups (OpenSSL or GPG), it can notify you about successful and/or failed backups (Mail or Twitter). It is very extensible and easy to add new functionality to. It&#8217;s easy to use.</p>
</blockquote>
<p>Check out the <a href="https://github.com/meskyanichi/backup/blob/develop/README.md" title="README">README</a> for all the details, but this allows you to backup your app via command line, pushing to S3 or rsync&#8217;ing to another server.  You can schedule it with the fantastic <a href="https://github.com/javan/whenever" title="Whenever gem">Whenever gem</a> too.</p>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2011/03/14/thebes-profiling-spork-backup-rubygem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A :limit of Rails&#8217; Migrations</title>
		<link>http://databasically.com/2011/03/01/a-limit-of-rails-migrations/</link>
		<comments>http://databasically.com/2011/03/01/a-limit-of-rails-migrations/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 16:35:51 +0000</pubDate>
		<dc:creator>Samuel Mullen</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=472</guid>
		<description><![CDATA[Migrations in Ruby on Rails use the &#8220;:limit&#8221; symbol to set the maximum length of the underlying field&#8217;s data type. Take for example, the following example migration: create_table :things do &#124;t&#124; t.string :name, :limit =&#62; 32 t.string :description t.timestamps end By default, Rails will create :description as data type &#8220;varchar(255)&#8221; and :name as &#8220;varchar(32)&#8221; in [...]]]></description>
			<content:encoded><![CDATA[<p>Migrations in Ruby on Rails use the &#8220;:limit&#8221; symbol to set the maximum length of the underlying field&#8217;s data type. Take for example, the following example migration:</p>
<pre name='code' class="ruby">create_table :things do |t|
  t.string :name, :limit =&gt; 32
  t.string :description
  t.timestamps
end
</pre>
<p>By default, Rails will create :description as data type &#8220;varchar(255)&#8221; and :name as &#8220;varchar(32)&#8221; in a MySQL database. But did you know you can set :limit to be greater than 255?</p>
<p>For whatever reason, many of us have gained the impression that 255 is the longest :string can be, but that just isn&#8217;t the case. If I wanted the :description field in the example above to be greater than 255, I could just define it as follows:</p>
<pre name='code' class="ruby">t.string :description, :limit =&gt; 1024
</pre>
<p>In fact, strings (i.e. varchars) in MySQL can hold up to 65,535 bytes of data.</p>
<p>The opinionated nature of  Ruby on Rails is a great asset in most instances, but we have to be careful not to let its opinions :limit us.</p>
<p><strong>Note:</strong> I&#8217;m pretty sure Rails sets the default limit of strings to be 255 for two reasons: 1) cross database compatability, and 2) MySQL&#8217;s InnoDB (utf-8) engine can&#8217;t index varchar fields exceeding 255 characters.</p>
<h3>Further Reading</h3>
<ul>
<li><a href="http://www.railsinside.com/misc/455-the-perils-of-opinionated-software-like-rails.html">The Perils Of Opinionated Software (like Rails)</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/data-types.html">MySQL Data Types</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/innodb-restrictions.html">Restrictions on InnoDB Tables</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/char.html">The CHAR and VARCHAR Data Types</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2011/03/01/a-limit-of-rails-migrations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>default_scope: Use Cases, Caveats, and Work Arounds</title>
		<link>http://databasically.com/2011/02/07/default_scope-use-cases-caveats-and-work-arounds/</link>
		<comments>http://databasically.com/2011/02/07/default_scope-use-cases-caveats-and-work-arounds/#comments</comments>
		<pubDate>Mon, 07 Feb 2011 14:57:09 +0000</pubDate>
		<dc:creator>Samuel Mullen</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[default_scope]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=432</guid>
		<description><![CDATA[In Ruby on Rails, named scopes are class methods used to restrict and organize the data searched for. In SQL terms, a named scope adds to the conditional (WHERE) and sorting (ORDER) sections of a query. See Railscast #108 for more information. In Rails 2.3.x, a new type of scoping was added to the API: [...]]]></description>
			<content:encoded><![CDATA[<p>In Ruby on Rails, named scopes are class methods used to restrict and organize the data searched for. In SQL terms, a named scope adds to the conditional (WHERE) and sorting (ORDER) sections of a query. See <a href="http://railscasts.com/episodes/108-named-scope">Railscast #108</a> for more information.</p>
<p>In Rails 2.3.x, a new type of scoping was added to the API: the default_scope. By using &#8220;default_scope&#8221;, one could restrict the data retrieved by every query without the need for extra method calls; it would just happen by &#8220;default&#8221;. If you wanted to have your data sorted in a particular manner, you could add &#8220;default_scope, :order =&gt; &#8216;created_at DESC&#8217;&#8221; to your model. From then on, all data retrieved would be ordered by &#8220;created_at&#8221; in a &#8220;descending&#8221; manner.</p>
<h2>Caveats</h2>
<p>In general, default_scope should be avoided if possible. Here are a few reasons:</p>
<ul>
<li>Out of site, out of mind: Because you don&#8217;t see that you are scoping your data as you query it, it&#8217;s easy to forget that it is in actuality being filtered. This can lead to a lot of head scratching until you remember the default_scope.</li>
<li>default_scope is inherited: All subclasses of the original model will inherit the default scoping. This may not be the behavior you desire.</li>
<li>Extra overhead: It&#8217;s one thing if you need your data sorted every single time, it&#8217;s quite another if you don&#8217;t. By using default_scope, you may be unnecessarily burdening your database.</li>
</ul>
<h2>Use Cases</h2>
<p>Like curry, default_scope is not inherently evil (obscure Phineas and Ferb reference), and there are instances where using it makes good sense. As an example, here at Databasically, we use default_scope to limit data retrieved by one application to a subset of what is available in a table. There will never be an instance where all the available data will be required, and so we limit it by default.</p>
<p>Here are the two use cases:</p>
<ul>
<li>When <strong>only</strong> a subset of the data is ever required</li>
<li>When the data must be returned in a specific order <strong>every</strong> time</li>
</ul>
<h2>Work Arounds</h2>
<p>I highlighted the words &#8220;only&#8221; and &#8220;every&#8221; in the section on use cases to make a point: default_scope should be used with caution. The fact is, however, that it&#8217;s not an &#8220;only&#8221; and &#8220;every&#8221; world, and as such, we need work arounds. In our case, it&#8217;s by using &#8220;with_exclusive_scope&#8221; and the undocumented &#8220;unscoped&#8221; (Rails 3.x only)</p>
<p>with_exclusive_scope example:</p>
<pre name='code' class="ruby">Article.with_exclusive_scope { find(:all) }
</pre>
<p>unscoped example (Rails 3.x only):</p>
<pre name='code' class="ruby">Article.unscoped
</pre>
<h2>Conclusion</h2>
<p>In general, we like to keep our code as DRY as possible, but in cases like default_scope, we prefer to be more explicit. We recognize the value of default_scope, but only use it when we absolutely need to, and even then we try to think of alternative methods.</p>
<p>How about you? We would really like to hear from you about your experiences with default scopings. What considerations do you take into account when choosing whether or not to use default_scope?</p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-default-scoping">What&#8217;s New in Edge Rails: Default Scoping</a></li>
<li><a href="http://stackoverflow.com/questions/2073419/overriding-default-scope-in-rails">Overriding default_scope in Rails</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2011/02/07/default_scope-use-cases-caveats-and-work-arounds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What time is it? Or, handling timezones in Rails.</title>
		<link>http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/</link>
		<comments>http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 03:46:59 +0000</pubDate>
		<dc:creator>Wes</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=406</guid>
		<description><![CDATA[As a followup to a stack overflow answer, I thought I would give some examples of working with time zones in rails. What does Rails timezone support do for me? Stores everything in UTC in the database Allows you to set an application default timezone and/or timezones for your users Automatically converts UTC in the [...]]]></description>
			<content:encoded><![CDATA[<p>As a followup to a <a href="http://stackoverflow.com/questions/3993619/time-select-with-12-hour-time-and-time-zone-in-ruby-on-rails">stack overflow answer</a>, I thought I would give some examples of working with time zones in rails.</p>
<h3>What does Rails timezone support do for me?</h3>
<ul>
<li>Stores everything in UTC in the database</li>
<li>Allows you to set an application default timezone and/or timezones for your users</li>
<li>Automatically converts UTC in the database to the correct zone and back</li>
</ul>
<h3>What zones are available?</h3>
<p>You can get a list of timezones with rake tasks:</p>
<pre name="code" class="ruby"># Displays names of all time zones recognized by the Rails TimeZone class, grouped by offset.
rake time:zones:all

# Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time
rake time:zones:local

# Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset.
rake time:zones:us</pre>
<h3>Setting the default time zone</h3>
<p>In your environment.rb (Rails 2) or application.rb (Rails 3) file, you can set the default timezone:</p>
<pre name="code" class="ruby">config.time_zone = 'Central Time (US &amp; Canada)'</pre>
<p>What does this do?  By setting an application-wide timezone, any datetime will be stored in UTC in the database, but will be translated when we access it.</p>
<h3>Set a timezone for a user</h3>
<p>You can use <a href="http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/time_zone_select">time_zone_select</a> to get a list of timezones for a user to pick from. The third argument is a list of &#8220;priority&#8221; zones that will appear first.</p>
<pre name="code" class="ruby"> time_zone_select( "user", 'time_zone', TimeZone.us_zones, :default =&gt; "Pacific Time (US &amp; Canada)")</pre>
<p>Once the value is saved in the database, you&#8217;ll want to set it for each request, per user:</p>
<pre name="code" class="ruby">  before_filter :set_timezone

  def set_timezone
    # current_user.time_zone #=&gt; 'Central Time (US &amp; Canada)'
    Time.zone = current_user.time_zone || 'Central Time (US &amp; Canada)'
  end</pre>
<p>[<strong>UPDATE</strong>: You'll need a field 'time_zone' in your user table!]</p>
<p>[<strong>UPDATE</strong>: You probably want to stay DRY and refer to the configured value instead of specifying the timezone value in both places: <br />Time.zone = current_user.time_zone || MyAppName::Application.config.time_zone]</p>
<p>Let me know if you have questions or improvements and I&#8217;ll integrate them into the article.  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2010/10/22/what-time-is-it-or-handling-timezones-in-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting the Runaround</title>
		<link>http://databasically.com/2010/10/21/getting-the-runaround/</link>
		<comments>http://databasically.com/2010/10/21/getting-the-runaround/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 19:03:23 +0000</pubDate>
		<dc:creator>Samuel Mullen</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Factories]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://databasically.com/?p=379</guid>
		<description><![CDATA[Yesterday was my third day on the job and to be honest, it wasn&#8217;t a lot of fun. I&#8217;m learning a lot of things right now: a new project, new methodologies, and new technologies. Combine all of that with a seemingly useless RSpec error and you can imagine where my blood pressure was reaching. I [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday was my third day on the job and to be honest, it wasn&#8217;t a lot of fun. I&#8217;m learning a lot of things right now: a new project, new methodologies, and new technologies. Combine all of that with a seemingly useless RSpec error and you can imagine where my blood pressure was reaching.</p>
<p>I had just added a new association to a FactoryGirl factory. I&#8217;ve not used FactoryGirl before, but it&#8217;s easy enough to copy and paste from other factories in the directory. So far, so good. I wasn&#8217;t watching the test output, but it was just one line, what could go wrong?</p>
<p>Several updates and commits later, I thought I&#8217;d better make sure we were still &#8220;Green&#8221;. Eh, not so much. Here&#8217;s the output I was seeing repeated over and over again.</p>
<pre name="code" class="ruby">
Failure/Error: Unable to find matching line from backtrace
stack level too deep
# /home/user/.rvm/gems/ruby-1.9.2-p0@xxx/gems/activerecord-3.0.0/lib/active_record/locking/optimistic.rb:62
</pre>
<p>I&#8217;ll not bore you with the details of my fruitless search to track this down. Suffice it to say, Wes &#8211; that would be my new boss &#8211; helped me back out my changes and track down the error.</p>
<p>It turns out that my &#8220;simple&#8221; addition to the &#8220;facility&#8221; factory wasn&#8217;t so simple. It actually resulted in a never ending loop of weeping and gnashing of teeth.</p>
<p>There are three models which were being dealt with: Facility, User, and Department. Departments have many users; facilities have many departments, and a facility can have a user who is defined as a contact. The schema looks like this:</p>
<p><a href="http://databasically.com/wp-content/uploads/2010/10/roundabout.png"><img class="alignnone size-full wp-image-383" title="runaround" src="http://databasically.com/wp-content/uploads/2010/10/roundabout.png" alt="rounaround schema" width="485" height="326" /></a></p>
<p>In my &#8220;facility&#8221; factory, I was making an association to the &#8220;users&#8221; table. Unbeknownst to me, the &#8220;user&#8221; factory was making an association to &#8220;department&#8221;, which was in turn making an association to &#8220;facility&#8221;, which was making an association to &#8220;user&#8221;, ad nauseum. The result: a &#8220;stack level too deep&#8221; error.</p>
<p>The solution was to just create the user association with the &#8220;department&#8221; set to nil.</p>
<p>Before:<br />
<code><br />
t.association :contact, :factory =&gt; :user<br />
</code></p>
<p>After:<br />
<code><br />
t.association :contact, :factory =&gt; :user, :department =&gt; nil<br />
</code></p>
<p>Hopefully this will make someone else&#8217;s first week on the job go a little smoother and keep them from getting the runaround.</p>
]]></content:encoded>
			<wfw:commentRss>http://databasically.com/2010/10/21/getting-the-runaround/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

