<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>I gotta have my orange juice. &#187; model</title>
	<atom:link href="http://scottmoonen.com/tag/model/feed/" rel="self" type="application/rss+xml" />
	<link>http://scottmoonen.com</link>
	<description>Jesu, Juva</description>
	<lastBuildDate>Thu, 31 May 2012 16:33:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='scottmoonen.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/71456a1f4695cc8129f159ece0f7b3a1?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>I gotta have my orange juice. &#187; model</title>
		<link>http://scottmoonen.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://scottmoonen.com/osd.xml" title="I gotta have my orange juice." />
	<atom:link rel='hub' href='http://scottmoonen.com/?pushpress=hub'/>
		<item>
		<title>Rails pattern: trim spaces on input</title>
		<link>http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/</link>
		<comments>http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/#comments</comments>
		<pubDate>Fri, 08 May 2009 16:53:51 +0000</pubDate>
		<dc:creator>Scott Moonen</dc:creator>
				<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[behavior]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://blog.fullvalence.com/?p=53</guid>
		<description><![CDATA[Problem: Your Rails application accepts user input for a number of models. For many or most of these fields, leading and trailing spaces are a significant inconvenience &#8212; they cause problems for your validators (email address, phone number, etc.) and they cause normalization and uniqueness problems in your database. Solution: Just as the Rails ActiveRecord [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottmoonen.com&#038;blog=9709237&#038;post=117&#038;subd=smoonen&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong>: Your Rails application accepts user input for a number of models.  For many or most of these fields, leading and trailing spaces are a significant inconvenience &#8212; they cause problems for your validators (email address, phone number, etc.) and they cause normalization and uniqueness problems in your database.</p>
<p><strong>Solution</strong>: Just as the Rails ActiveRecord class uses methods like <tt>belongs_to</tt> and <tt>validates_format_of</tt> to define model relationships and behaviors, create a new class method to express trimming behavior.  There are a number of ways to do this; I will present one possibility that I have used in my own code.  I created a file <tt>lib/trimmer.rb</tt> with the following contents:</p>
<p><pre class="brush: ruby;">
module Trimmer
  # Make a class method available to define space-trimming behavior.
  def self.included base
    base.extend(ClassMethods)
  end

  module ClassMethods
    # Register a before-validation handler for the given fields to
    # trim leading and trailing spaces.
    def trimmed_fields *field_list
      before_validation do |model|
        field_list.each do |n|
          model[n] = model[n].strip if model[n].respond_to?('strip')
        end
      end
    end
  end
end
</pre></p>
<p>Then I write the following in my models:</p>
<p><pre class="brush: ruby;">
require 'trimmer'
class MyModel &amp;lt; ActiveRecord::Base
  include Trimmer
  . . .
  trimmed_fields :first_name, :last_name, :email, :phone
  . . .
end
</pre></p>
<p>While this makes the behavior available to particular models explicitly, you may prefer to make this behavior available to all of your models implicitly.  In that case, you can extend the <tt>ActiveRecord::Base</tt> class behavior by adding the following to <tt>config/environment.rb</tt>:</p>
<p><pre class="brush: ruby;">
require 'trimmer'
class ActiveRecord::Base
  include Trimmer
end
</pre></p>
<p>If you do this, the <tt>trimmed_fields</tt> class method will be available to all of your models.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/smoonen.wordpress.com/117/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/smoonen.wordpress.com/117/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/smoonen.wordpress.com/117/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=scottmoonen.com&#038;blog=9709237&#038;post=117&#038;subd=smoonen&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://scottmoonen.com/2009/05/08/rails-pattern-trim-spaces-on-input/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c801efd19dadd22c4f35d3f1f6ea1869?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">smoonen</media:title>
		</media:content>
	</item>
	</channel>
</rss>
