<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Chris Finne - Bay Area Ruby on Rails Developer for Startups</title>
  <id>http://127.0.0.1</id>
  <updated>2010-01-17T00:00:00Z</updated>
  <author>
    <name></name>
  </author>
  <entry>
    <title>Ruby - Is a Phone Number Valid for SMS?</title>
    <link href="http://127.0.0.1/2011/12/05/ruby---is-a-phone-number-valid-for-sms/" rel="alternate"/>
    <id>http://127.0.0.1/2011/12/05/ruby---is-a-phone-number-valid-for-sms/</id>
    <published>2011-12-05T00:00:00Z</published>
    <updated>2011-12-05T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;div style="clear:both"&gt;
I've used an algorithm like this in a few projects, so thought I'd post it for posterity...

&lt;script src="https://gist.github.com/1446662.js?file=gistfile1.rb"&gt;&lt;/script&gt;

</summary>
    <content type="html">&lt;div style="clear:both"&gt;
I've used an algorithm like this in a few projects, so thought I'd post it for posterity...

&lt;script src="https://gist.github.com/1446662.js?file=gistfile1.rb"&gt;&lt;/script&gt;

&lt;/div&gt;


&lt;p&gt;I&amp;rsquo;ve used it for integrating with &lt;a href="http://www.aerialink.com/"&gt;SMS Solutions on Aerialink&lt;/a&gt; and &lt;a href="http://aerialink.com/"&gt;Twilio&amp;rsquo;s API&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Parse American Dates with Ruby 1.9 (in Rails)</title>
    <link href="http://127.0.0.1/2011/10/15/parse-american-dates-with-ruby-19-in-rails/" rel="alternate"/>
    <id>http://127.0.0.1/2011/10/15/parse-american-dates-with-ruby-19-in-rails/</id>
    <published>2011-10-15T00:00:00Z</published>
    <updated>2011-10-15T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: This method no longer works as of Ruby 1.9.3. Try Jeremy Evans' GEM &lt;a href="https://github.com/jeremyevans/ruby-american_date"&gt;ruby american date&lt;/a&gt; that works with all 1.9.x versions&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: This method no longer works as of Ruby 1.9.3. Try Jeremy Evans' GEM &lt;a href="https://github.com/jeremyevans/ruby-american_date"&gt;ruby american date&lt;/a&gt; that works with all 1.9.x versions.&lt;/p&gt;

&lt;p&gt;I probably spent days peeling the onion back to this solution, so had to share to avoid others from suffering the pain.&lt;/p&gt;

&lt;p&gt;Ruby 1.8 happily parses American dates, but Ruby 1.9 refuses. When it comes to software, I&amp;rsquo;m not an ideologue. I just want to get the job done, so I won&amp;rsquo;t pontificate, bloviate, opine or ramble on this topic. I&amp;rsquo;ll find the workaround and move on with my life.&lt;/p&gt;

&lt;p&gt;In a Rails project running on Ruby 1.9, I need to parse date and date/time strings that have the American format of month/day/year. I needed to do this in pure Ruby cases as well as assigning Date and Date/Time strings to ActiveRecord fields.&lt;/p&gt;

&lt;p&gt;I tried parsing the strings before they got to ActiveRecord with stuff like:&lt;/p&gt;

&lt;pre&gt;
  Time.strptime(params[:start_at],"%m/%d/%Y %I:%M %p")
&lt;/pre&gt;


&lt;p&gt;But there are too many places to put this and this is a fragile and error-prone approach. I then found some monkey-patching solutions to work with just date strings &lt;a href="http://blog.mustmodify.com/2011/03/get-ruby-1-9-date-parse-to-assume-american-date-format.html"&gt;here&lt;/a&gt; and &lt;a href="https://gist.github.com/779859"&gt;here&lt;/a&gt;, but this still didn&amp;rsquo;t solve the Date/Time strings.&lt;/p&gt;

&lt;p&gt;I tried attacking the Date/Time string conversion in &lt;code&gt;ActiveRecord::ConnectionAdapters::Column&lt;/code&gt; similar to &lt;a href="http://blog.nominet.org.uk/tech/2007/06/14/date-and-time-formating-issues-in-ruby-on-rails/"&gt;here&lt;/a&gt; by overriding &lt;code&gt;string_to_time&lt;/code&gt; and &lt;code&gt;fast_string_to_time&lt;/code&gt;, but still had weird and inconsistent results when assigning a Date/Time string to an ActiveRecord attribute.&lt;/p&gt;

&lt;p&gt;The final solution is of course a fraction of the code I cycled through and oh so simple, just swap the month and day if the string you are parsing looks like xx/xx/xxxx. I&amp;rsquo;m hooking in a a VERY low level, Ruby&amp;rsquo;s &lt;code&gt;Date._parse()&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;
  class Date
    class &lt;&lt; self
      def _parse_with_american(str, comp=true)
        hash = _parse_without_american(str, comp)
        if str=~/\A(\d{1,2})\/(\d{1,2})\/(\d{4})/
          tmp = hash[:mon]
          hash[:mon] = hash[:mday]
          hash[:mday] = tmp
        end
        hash
      end
      alias_method_chain :_parse, :american
    end
  end
&lt;/pre&gt;


&lt;p&gt;I&amp;rsquo;m using &lt;a href="https://github.com/rails/rails/blob/13e00ce6064fd1ce143071e3531e65f64047b013/activesupport/lib/active_support/core_ext/module/aliasing.rb#L23"&gt;alias_method_chain&lt;/a&gt; from Rails, but if you are not using Rails, well, you&amp;rsquo;ll figure it out.&lt;/p&gt;

&lt;p&gt;I have no idea why there is a &lt;code&gt;Date.parse&lt;/code&gt; and a &lt;code&gt;Date._parse&lt;/code&gt;, nor do I care at this point. I&amp;rsquo;m just glad to move on to solving real problems rather than wrestling with date/time strings.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Rails 3 to_s(:db) when database time is NOT UTC</title>
    <link href="http://127.0.0.1/2011/09/23/rails-3-tosdb-when-database-time-is-not-utc/" rel="alternate"/>
    <id>http://127.0.0.1/2011/09/23/rails-3-tosdb-when-database-time-is-not-utc/</id>
    <published>2011-09-23T00:00:00Z</published>
    <updated>2011-09-23T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;p&gt;I&amp;rsquo;m upgrading a Rails application from v2.x to v3.1 and the application stores its dates in MySQL in Pacific timezone rather than UTC.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve set my config/application.rb with the following settings&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;I&amp;rsquo;m upgrading a Rails application from v2.x to v3.1 and the application stores its dates in MySQL in Pacific timezone rather than UTC.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve set my config/application.rb with the following settings&lt;/p&gt;

&lt;pre&gt;
config.time_zone = 'Pacific Time (US &amp; Canada)'
config.active_record.default_timezone = :local
&lt;/pre&gt;


&lt;p&gt;However, I just discovered that ActiveSupport time extensions don&amp;rsquo;t really care about ActiveRecord&amp;rsquo;s default_timezone setting.&lt;/p&gt;

&lt;pre&gt;
rails31&gt;&gt; Time.now.to_s(:db)
=&gt; "2011-09-23 03:02:52"
rails31&gt;&gt; 1.second.ago.to_s(:db)
=&gt; "2011-09-23 10:02:59"
&lt;/pre&gt;


&lt;p&gt;WHAT??? I know that these are 2 different classes:&lt;/p&gt;

&lt;pre&gt;
rails31&gt;&gt; Time.now.class
=&gt; Time
rails31&gt;&gt; 1.second.ago.class
=&gt; ActiveSupport::TimeWithZone
&lt;/pre&gt;


&lt;p&gt;This is because ActiveSupport sets to UTC before doing the &amp;ldquo;to_s&amp;rdquo; which makes sense with the default Rails configuration where all dates are stored in the DB as UTC.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/time_with_zone.rb"&gt;https://github.com/rails/rails/blob/master/activesupport/lib/active_support/time_with_zone.rb&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;
# &lt;tt&gt;:db&lt;/tt&gt; format outputs time in UTC; all others output time in local.
# Uses TimeWithZone's +strftime+, so &lt;tt&gt;%Z&lt;/tt&gt; and &lt;tt&gt;%z&lt;/tt&gt; work correctly.
def to_s(format = :default)
  if format == :db
    utc.to_s(format)
  elsif formatter = ::Time::DATE_FORMATS[format]
    formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
  else
    "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format
  end
end
alias_method :to_formatted_s, :to_s
&lt;/pre&gt;


&lt;p&gt;I guess I understand this because I&amp;rsquo;m dealing with 2 different libraries: ActiveRecord vs ActiveSupport, but it sure threw me for a loop in some of my tests where I would sometimes create values with Time.now and other times with 1.day.ago and the time-zone conversion would fail the test.&lt;/p&gt;

&lt;p&gt;My solution to this issue is to start using .to_s(:localdb) rather than .to_s(:db)&lt;/p&gt;

&lt;pre&gt;
Time::DATE_FORMATS.merge!(:localdb=&gt;"%Y-%m-%d %H:%M:%S")
&lt;/pre&gt;




&lt;pre&gt;
rails31&gt;&gt; Time.now.to_s(:db)
=&gt; "2011-09-23 02:56:34"
rails31&gt;&gt; Time.now.to_s(:localdb)
=&gt; "2011-09-23 02:56:38"
rails31&gt;&gt; 1.second.ago.to_s(:db)
=&gt; "2011-09-23 10:02:59"
rails31&gt;&gt; 1.second.ago.to_s(:localdb)
=&gt; "2011-09-23 03:02:59"
&lt;/pre&gt;

</content>
  </entry>
  <entry>
    <title>Ruby Rails Initialize BigDecimal with commas in the String</title>
    <link href="http://127.0.0.1/2011/07/22/ruby-rails-initialize-bigdecimal-with-commas-in-the-string/" rel="alternate"/>
    <id>http://127.0.0.1/2011/07/22/ruby-rails-initialize-bigdecimal-with-commas-in-the-string/</id>
    <published>2011-07-22T00:00:00Z</published>
    <updated>2011-07-22T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;p&gt;Came across a bug in the app while helping out on &lt;a href="http://www.getspotme.com/"&gt;SpotMe&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When you create a BigDecimal, it takes a String as the argument (not a Float or Fixnum)&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;Came across a bug in the app while helping out on &lt;a href="http://www.getspotme.com/"&gt;SpotMe&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When you create a BigDecimal, it takes a String as the argument (not a Float or Fixnum)&lt;/p&gt;

&lt;pre&gt;
BigDecimal.new('99')
BigDecimal.new('44.23')
&lt;/pre&gt;


&lt;p&gt;In &lt;a href="http://www.getspotme.com/"&gt;SpotMe&lt;/a&gt; when someone was entering an amount for a bill or a payment, the amount might have a comma in it. The problem is that BigDecimal ignores everything to the right of the first comma it encounters&lt;/p&gt;

&lt;pre&gt;
&gt;&gt; BigDecimal.new('1,200') == BigDecimal.new('1200')
=&gt; false

&gt;&gt; BigDecimal.new('1,200') == BigDecimal.new('1')
=&gt; true

&lt;/pre&gt;


&lt;p&gt;Monkey Patch to the rescue!&lt;/p&gt;

&lt;script src="https://gist.github.com/1099504.js?file=gistfile1.rb"&gt;&lt;/script&gt;




&lt;pre&gt;
&gt;&gt; BigDecimal.new('1,200') == BigDecimal.new('1200')
=&gt; true

&gt;&gt; BigDecimal.new('1,200') == BigDecimal.new('1')
=&gt; false

&lt;/pre&gt;


&lt;p&gt;While this concept can work for just Ruby, I used Rails' &lt;a href="http://weblog.rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain"&gt;alias_method_chain&lt;/a&gt;. I&amp;rsquo;ve read some &lt;a href="http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/"&gt;arguments&lt;/a&gt; against this &lt;a href="http://stackoverflow.com/questions/3689736/rails-3-alias-method-chain-still-used"&gt;approach&lt;/a&gt;, but in the Startup world, I&amp;rsquo;m often just trying to get the job done fast, so I like to use it.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Screen Free Day</title>
    <link href="http://127.0.0.1/2010/10/19/screen-free-day/" rel="alternate"/>
    <id>http://127.0.0.1/2010/10/19/screen-free-day/</id>
    <published>2010-10-19T00:00:00Z</published>
    <updated>2010-10-19T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;p&gt;A letter came home from my son&amp;rsquo;s 2nd grade class advocating a &amp;ldquo;Wellness Week&amp;rdquo; where physical activity, nutrition, etc. are emphasized. No problems there, except that they implicitly single out technology as an enemy of health&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;A letter came home from my son&amp;rsquo;s 2nd grade class advocating a &amp;ldquo;Wellness Week&amp;rdquo; where physical activity, nutrition, etc. are emphasized. No problems there, except that they implicitly single out technology as an enemy of health.&lt;/p&gt;

&lt;p&gt;Following is my response on the class&amp;rsquo;s Shutterfly forum:&lt;/p&gt;

&lt;blockquote&gt;
I'll freely admit that we failed this miserably yesterday. While I understand promoting physical activity to improve health, I'm a vocal advocate against anti-technology campaigns like this. We live in the capital of technology where our local economy is driven by media, video games and computers. My own career in software was sparked as a kid when I re-programmed my video games.
&lt;/blockquote&gt;




&lt;blockquote&gt;
Our kids should have a healthy passion for these technologies as they make learning easier, more engaging and more accessible. It is hard to get my 2 year old to play a board game, but there are many educational games on the iPad that have enough flash to keep her attention and an interface simple enough for a toddler.
&lt;/blockquote&gt;




&lt;blockquote&gt;
I'd rather see a "handwriting-free day" where all homework and drawing is done on a computer. Handwriting is a skill that is no longer relevant in most careers and in most of life. A "book-free day" where all reading is done on a piece of technology would also be helpful. By the time our kids enter college or the workforce, there will be very few printed periodicals no more printed newspapers. The sooner our kids get used to reading and learning on devices like the iPad, Kindle and computers, the better prepared they will be for future education and employment.

&lt;/blockquote&gt;


&lt;p&gt;It also reminds me of a great Onion post about &lt;a href="http://www.theonion.com/articles/report-90-of-waking-hours-spent-staring-at-glowing,2747/"&gt;Glowing Rectangles&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Top FAILS from Apples Mac OSX</title>
    <link href="http://127.0.0.1/2010/09/23/top-fails-from-apples-mac-osx/" rel="alternate"/>
    <id>http://127.0.0.1/2010/09/23/top-fails-from-apples-mac-osx/</id>
    <published>2010-09-23T00:00:00Z</published>
    <updated>2010-09-23T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;

&lt;div style="float:right"&gt;&lt;img src="/images/imac.png" width="200"&gt;&lt;/img&gt;&lt;/div&gt;


&lt;p&gt;Just a running list of simple concepts that Microsoft had nailed years ago, but Apple still FAILS to get&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;

&lt;div style="float:right"&gt;&lt;img src="/images/imac.png" width="200"&gt;&lt;/img&gt;&lt;/div&gt;


&lt;p&gt;Just a running list of simple concepts that Microsoft had nailed years ago, but Apple still FAILS to get.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Close Window (CMD-W) and Quit Application (CMD-Q) keys are adjacent. How many times have I mis-typed and lost all my windows in TextMate? Thank god Chrome finally implemented a feature to avoid this problem.&lt;/li&gt;
&lt;li&gt;Can only resize a window from a single corner. Did the other 3 sides cost too much to implement?&lt;/li&gt;
&lt;li&gt;Apple hates the keyboard &amp;ndash; inconsistent short-cut keys and menu navigation.&lt;/li&gt;
&lt;li&gt;the &amp;ldquo;Green circle of confusion&amp;rdquo;. I want to maximize or unmaximize a window. When I click that green circle, it seems that someone is rolling the dice to decide what should happen. This seems to have gotten better with Snow Leopard, but still&amp;hellip;&lt;/li&gt;
&lt;li&gt;Going from Tiger to Leopard was ok, but Leopard to Snow Leopard? What&amp;rsquo;s next? Striped Snow Leopard? (Update: ok, they release Lion. Next one better not be Snow Lion, or Desert Lion)&lt;/li&gt;
&lt;li&gt;Snappy. I still think my old Windows XP systems had a more &amp;ldquo;snappy&amp;rdquo; / &amp;ldquo;quick&amp;rdquo; feel to the windowing environment, e.g. opening, closing, moving windows, etc.&lt;/li&gt;
&lt;/ol&gt;

</content>
  </entry>
  <entry>
    <title>Apple Showdown Magic Mouse vs Magic Trackpad</title>
    <link href="http://127.0.0.1/2010/09/04/apple-showdown-magic-mouse-vs-magic-trackpad/" rel="alternate"/>
    <id>http://127.0.0.1/2010/09/04/apple-showdown-magic-mouse-vs-magic-trackpad/</id>
    <published>2010-09-04T00:00:00Z</published>
    <updated>2010-09-04T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;

&lt;h3&gt;Apple Mice&lt;/h3&gt;

&lt;div style="float:right"&gt;&lt;img src="/images/magic-mouse.jpg" width="200"&gt;&lt;/img&gt;&lt;/div&gt;


&lt;p&gt;I&amp;rsquo;ve never liked Apple&amp;rsquo;s Mice (Mouses? Meeces?). They typically take an act of god to &amp;ldquo;click&amp;rdquo; and the Magic Mouse is no different. They are also usually a lot heavier than the popular models in the PC world. But the Magic Mouse came with my Mac Pro I got a few months ago and I decided to give it a shot; afterall, Apple does so many things right (with a few glaring exceptions), they always deserve a chance&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;

&lt;h3&gt;Apple Mice&lt;/h3&gt;

&lt;div style="float:right"&gt;&lt;img src="/images/magic-mouse.jpg" width="200"&gt;&lt;/img&gt;&lt;/div&gt;


&lt;p&gt;I&amp;rsquo;ve never liked Apple&amp;rsquo;s Mice (Mouses? Meeces?). They typically take an act of god to &amp;ldquo;click&amp;rdquo; and the Magic Mouse is no different. They are also usually a lot heavier than the popular models in the PC world. But the Magic Mouse came with my Mac Pro I got a few months ago and I decided to give it a shot; afterall, Apple does so many things right (with a few glaring exceptions), they always deserve a chance.&lt;/p&gt;

&lt;p&gt;The Magic Mouse weighs in at around 100 grams with the 2 AA batteries (about 94 grams if the batteries aren&amp;rsquo;t fully charged) which I was surprised is about the same weight as one of my standard single AA battery mice from the PC world. It feels twice the weight, but scales don&amp;rsquo;t lie. There must be some Steve Jobs Faerie Dust going on here.&lt;/p&gt;

&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;

&lt;h3&gt;Apple Laptop Trackpads&lt;/h3&gt;

&lt;div style="float:right"&gt;&lt;img src="/images/macbook-pro.png" width="200"&gt;&lt;/img&gt;&lt;/div&gt;


&lt;p&gt;I&amp;rsquo;ve had numerous Macbook Pro&amp;rsquo;s and Airs since I converted 3 years ago. I&amp;rsquo;ve loved all the trackpads and they have improved them with every model. I&amp;rsquo;ve always found laptop trackpads to be the most productive as your hand doesn&amp;rsquo;t have to leave the keyboard for many operations. So I was extremely excited when I saw the &lt;a href="http://twitter.com/#search?q=%23magic%20%23trackpad"&gt;Trackpad mentions on Twitter&lt;/a&gt;, so I hardly looked at the price before ordering in &lt;a href="http://theoatmeal.com/blog/apps"&gt;classic Oatmeal psychology&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;

&lt;h3&gt;Apple Magic Trackpads&lt;/h3&gt;

&lt;div style="float:right"&gt;&lt;img src="/images/trackpad.png" width="200"&gt;&lt;/img&gt;&lt;/div&gt;


&lt;p&gt;My first impression if the Magic Trackpad is that the area is a lot larger than I expected as I was used to the laptop-sized surfaces, but after using, the size wasn&amp;rsquo;t much of an issue.&lt;/p&gt;

&lt;p&gt;What I did find challenging is that I had to lift my had so far to use it. Granted, it was the same distance as moving my hand to the mouse, but I was subconsciously expecting it to be as easy as the movement to the laptop trackpad.&lt;/p&gt;

&lt;p&gt;&lt;img src="/images/keyboard.png" width="200"&gt;&lt;/img&gt; Although I&amp;rsquo;m right handed, I mouse left-handed which is an advantage as the Apple keyboard extra keys extend pretty far to the right, which would make hand movements much farther.&lt;/p&gt;

&lt;p&gt;So I tried aligning the Trackpad in between my hands, just below the keyboard, but the Trackpad is too big, would occasionally get brushed and its angled elevation got in the way.&lt;/p&gt;

&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;

&lt;h3&gt;The Winner &amp;ndash; Better Touch Tool + Magic Mouse&lt;/h3&gt;

&lt;div style="float:right;margin-top:10px"&gt;&lt;img src="/images/better-touch-tool.png" width="200"&gt;&lt;/img&gt;&lt;/div&gt;


&lt;p&gt;Although it worked well on its own, I gave up on the Trackpad because it the ergonomics just didn&amp;rsquo;t fit for me. I&amp;rsquo;m using the Magic Mouse with the &lt;a href="http://blog.boastr.net/"&gt;Better Touch Tool&lt;/a&gt;. This tool allows you to make the surface of the Magic Mouse behave like the Trackpad, i.e. touch-clicks. It is VERY customizable, allowing you to define what areas on the Magic Mouse correspond to what actions, e.g. left-click right-click or dead zone.&lt;/p&gt;

&lt;h3&gt;&amp;nbsp;&lt;/h3&gt;
</content>
  </entry>
  <entry>
    <title>Rails Fixture Name Starts With Test</title>
    <link href="http://127.0.0.1/2010/08/16/rails-fixture-name-starts-with-test/" rel="alternate"/>
    <id>http://127.0.0.1/2010/08/16/rails-fixture-name-starts-with-test/</id>
    <published>2010-08-16T00:00:00Z</published>
    <updated>2010-08-16T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;p&gt;I&amp;rsquo;m using Rails 2.3.5 and I have a Rails model called &amp;ldquo;Testimonial&amp;rdquo;. Whenever I run my tests, I get a funky error about ActionView::TestCase.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
desktop:~/code/project chrisfinne $ ruby -Ilib:test test/unit/business_test.rb&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;I&amp;rsquo;m using Rails 2.3.5 and I have a Rails model called &amp;ldquo;Testimonial&amp;rdquo;. Whenever I run my tests, I get a funky error about ActionView::TestCase.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
desktop:~/code/project chrisfinne $ ruby -Ilib:test test/unit/business_test.rb
Loaded suite test/unit/business_test
Started
&amp;hellip;E&amp;hellip;.
Finished in 0.311574 seconds.&lt;/p&gt;

&lt;p&gt;  1) Error:
testimonials(ActionView::TestCase):
TypeError: wrong argument type Class (expected Module)
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found this &lt;a href="https://rails.lighthouseapp.com/projects/8994/tickets/261-argumenterror-using-fixtures-named-test"&gt;Lighthouse ticket&lt;/a&gt; that tells me that I can&amp;rsquo;t have models called Test (which I already knew) and I cannot have any fixture files that &lt;em&gt;START with&lt;/em&gt; &amp;ldquo;test&amp;rdquo;, e.g. &amp;ldquo;&lt;strong&gt;test&lt;/strong&gt;imonials.yml&amp;rdquo;. I didn&amp;rsquo;t know that when I chose the name for my class. The fix is pretty easy&amp;hellip;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rename the fixture file from &amp;ldquo;test/fixtures/testimonials.yml&amp;rdquo; to something else like &amp;ldquo;test/fixtures/&lt;strong&gt;a&lt;/strong&gt;testimonials.yml&amp;rdquo;&lt;/li&gt;
&lt;li&gt;Associate the fixture with the proper class in test/test_helper.rb&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&lt;code&gt;
  class ActiveSupport::TestCase&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;...
set_fixture_class :atestimonials =&amp;gt; Testimonial
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;  end
&lt;/code&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Indexing Sphinx on Amazon AWS EC2 With MySQL RDS and Compression</title>
    <link href="http://127.0.0.1/2010/06/03/indexing-sphinx-on-amazon-aws-ec2-with-mysql-rds-and-compression/" rel="alternate"/>
    <id>http://127.0.0.1/2010/06/03/indexing-sphinx-on-amazon-aws-ec2-with-mysql-rds-and-compression/</id>
    <published>2010-06-03T00:00:00Z</published>
    <updated>2010-06-03T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;p&gt;I&amp;rsquo;m using &lt;a href="http://aws.amazon.com/rds/"&gt;Amazon&amp;rsquo;s Relational Database Service&lt;/a&gt; which runs MySQL 5.1 on a Large EC2 instance for me.&lt;/p&gt;

&lt;p&gt;I have over 2 million records in a Rails application that I index with Sphinx&amp;hellip;&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;I&amp;rsquo;m using &lt;a href="http://aws.amazon.com/rds/"&gt;Amazon&amp;rsquo;s Relational Database Service&lt;/a&gt; which runs MySQL 5.1 on a Large EC2 instance for me.&lt;/p&gt;

&lt;p&gt;I have over 2 million records in a Rails application that I index with Sphinx.&lt;/p&gt;

&lt;p&gt;Now I&amp;rsquo;m looking at MySQL Compression.
Sphinx can use it with this switch from the &lt;a href="http://sphinxsearch.com/docs/current.html"&gt;Sphinx Docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
mysql_connect_flags = 32 # enable compression
&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;Sphinx Indexing (to a RAM disk) w/o compression&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;
real    15m17.159s&lt;br/&gt;
user    14m20.330s&lt;br/&gt;
sys 0m28.630s
&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;Sphinx Indexing (to a RAM disk) w/ MySQL Compression:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;
real    15m45.433s&lt;br/&gt;
user    14m58.080s&lt;br/&gt;
sys 0m13.310s
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So no material difference between the two.&lt;/p&gt;

&lt;p&gt;As an aside:&lt;/p&gt;

&lt;h3&gt;Sphinx Indexing (to an EBS boot disk) w/ MySQL Compression:&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;
real    16m0.305s&lt;br/&gt;
user    14m56.730s&lt;br/&gt;
sys 0m16.230s
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;so it looks like using the RAM disk on EC2 doesn&amp;rsquo;t affect indexing performance all that much.&lt;/p&gt;

&lt;h3&gt;MySQL Compression for Rails&lt;/h3&gt;

&lt;p&gt;In parallel I was also looking up &lt;a href="/2010/06/03/how-to-enable-mysql-compression-with-rails-and-the-mysql-gem/"&gt;how to enable MySQL compression in a Rails app using the MySQL gem&lt;/a&gt;&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>How to Enable MySQL Compression With Rails and the MySQL Gem</title>
    <link href="http://127.0.0.1/2010/06/03/how-to-enable-mysql-compression-with-rails-and-the-mysql-gem/" rel="alternate"/>
    <id>http://127.0.0.1/2010/06/03/how-to-enable-mysql-compression-with-rails-and-the-mysql-gem/</id>
    <published>2010-06-03T00:00:00Z</published>
    <updated>2010-06-03T00:00:00Z</updated>
    <author>
      <name></name>
    </author>
    <summary type="html">&lt;p&gt;While looking to optimize my Rails performance on Amazon EC2 and RDS, I wanted to experiment to see if there are any
performance improvements with enabling MySQL compression in the connection protocol since with RDS, the MySQL instance&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;While looking to optimize my Rails performance on Amazon EC2 and RDS, I wanted to experiment to see if there are any
performance improvements with enabling MySQL compression in the connection protocol since with RDS, the MySQL instance
is running on another machine and I&amp;rsquo;m unsure how far away that machine really is. (Spoiler: it seems to worsen performance with EC2 and RDS)&lt;/p&gt;

&lt;p&gt;A couple years ago, I wanted to use MySQL Stored Procedures with a Rails app and found that Rails didn&amp;rsquo;t support it.
The default MySQL connection from ActiveRecord didn&amp;rsquo;t have the proper flag (Mysql::CLIENT_MULTI_RESULTS) enabled to deal with the results from a stored procedure.
This is discussion is currently captured in &lt;a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3151"&gt;Lighthouse&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It looks like the flag (Mysql::CLIENT_MULTI_RESULTS) is now enabled by default in Rails 2.3.5 (maybe a bit earlier)&lt;/p&gt;

&lt;p&gt;If you look in: &lt;strong&gt;activerecord/lib/active_record/connection_adapters/mysql_adapter.rb&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ActiveRecord::Base&lt;/strong&gt; class method &lt;strong&gt;mysql_connection&lt;/strong&gt;
about line #73&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
default_flags = Mysql.const_defined?(:CLIENT_MULTI_RESULTS) ? Mysql::CLIENT_MULTI_RESULTS : 0
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Do a monkey patch and bitwise OR the flags to change it to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
default_flags = Mysql::CLIENT_MULTI_RESULTS | Mysql::CLIENT_COMPRESS
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(assuming your MySQL gem is installed and supports both flags like the MySQL 2.8.1 gem does)&lt;/p&gt;

&lt;p&gt;I then did somewhat unscientific comparision with and without the flag&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
./script/runner &amp;lsquo;puts Benchmark.realtime{Business.all(:limit=&gt;10000)}.to_s&amp;rsquo;
&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;Results&lt;/h3&gt;

&lt;p&gt;With the patch, the test typically took 1.6 seconds. Without the patch it typically took 0.6 seconds. So it looks like &lt;strong&gt;the added compression
adds more overhead on the CPU than what you gain in network savings&lt;/strong&gt; when dealing with EC2 and RDS in the same availability zone.&lt;/p&gt;

&lt;p&gt;So as I saw with &lt;a href="/2010/06/03/indexing-sphinx-on-amazon-aws-ec2-with-mysql-rds-and-compression/"&gt;MySQL compression enabled in my Sphinx setup&lt;/a&gt;
the network bandwidth between RDS and EC2 instances isn&amp;rsquo;t the bottleneck I&amp;rsquo;m looking for.&lt;/p&gt;
</content>
  </entry>
</feed>

