<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>This Old Coder - Home</title>
  <id>tag:thisoldcoder.com,2008:mephisto/</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://thisoldcoder.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://thisoldcoder.com/" rel="alternate" type="text/html"/>
  <updated>2008-03-20T03:01:34Z</updated>
  <entry xml:base="http://thisoldcoder.com/">
    <author>
      <name>thisoldcoder</name>
    </author>
    <id>tag:thisoldcoder.com,2008-03-19:2</id>
    <published>2008-03-19T22:38:00Z</published>
    <updated>2008-03-20T03:01:34Z</updated>
    <category term="&quot;sql server&quot;"/>
    <category term="rails"/>
    <link href="http://thisoldcoder.com/2008/3/19/ms-sql-server-and-those-pesky-nulls" rel="alternate" type="text/html"/>
    <title>MS SQL Server and Those Pesky NULLs</title>
<content type="html">
            So I'm working on a Rails application with a MS SQL Server 2005 back-end. The application needs to access the Dynamics GP data stored on that server, and while I could have other tables (users, sessions, etc) stored separately in, say MySQL, I elected to keep them all together, at least for the time being. (I'll detail the steps I used to get &lt;a href=&quot;http://www.unixodbc.org/&quot;&gt;unixODBC&lt;/a&gt; and &lt;a href=&quot;http://www.freetds.org/&quot;&gt;FreeTDS&lt;/a&gt; installed and working on &lt;a href=&quot;http://www.openbsd.org/&quot;&gt;OpenBSD 4.2&lt;/a&gt; in another post. In the meantime, &lt;a&gt;this is the Rails wiki article&lt;/a&gt; that got me started.

Anyways, back to the problem I ran into. Parts of the application follow the standard Rails new/create idiom. A user clicks on the New Foobar link, and gets taken to a blank form to fill-out. However, with this unixODBC/FreeTDS/MS SQL setup, all of my text fields were winding defaulting not to blanks, but to the text string &quot;NULL&quot;.

A quick search on Google turned up &lt;a href=&quot;http://dev.rubyonrails.org/ticket/9469&quot;&gt;this issue&lt;/a&gt; on Rails Trac, along with a temporary monkeypatch fix posted in the comments by meekish:

&lt;pre class=&quot;ruby&quot;&gt;ActiveRecord::Base.class_eval do
  def attributes_from_column_definition_with_null_fix
    returning attributes = attributes_from_column_definition_without_null_fix do
      attributes.each_value { |value| value.replace('') if value == 'NULL' }
    end
  end
	
  alias_method_chain :attributes_from_column_definition, :null_fix
end&lt;/pre&gt;

He suggested putting this in a lib/ file, and requiring that file in environment.rb.

This was cool, and it &lt;b&gt;almost&lt;/b&gt; met my needs. Being picky, I wasn't quite satisfied with replacing a &quot;NULL&quot; string with a blank. It's supposed to represent a null value. Then there's a more practical issue: if new action created a new object, saved it, and immediately redirected to an edit action (long story), Ruby would scream.

So here's my version of this monkeypatch that instead replaces &quot;NULL&quot; strings with nils:

&lt;pre class=&quot;ruby&quot;&gt;ActiveRecord::Base.class_eval do
  def attributes_from_column_definition_with_null_fix
    returning attributes = attributes_from_column_definition_without_null_fix do
      attributes.each_pair do |key, value| 
        attributes[key] = nil if value == 'NULL'
      end
    end
  end
	
  alias_method_chain :attributes_from_column_definition, :null_fix
end&lt;/pre&gt;

Works like a charm.
          </content>  </entry>
  <entry xml:base="http://thisoldcoder.com/">
    <author>
      <name>thisoldcoder</name>
    </author>
    <id>tag:thisoldcoder.com,2008-03-18:1</id>
    <published>2008-03-18T19:56:00Z</published>
    <updated>2008-03-20T03:20:01Z</updated>
    <category term="mvc"/>
    <category term="rails"/>
    <category term="ruby"/>
    <link href="http://thisoldcoder.com/2008/3/18/food-goes-in-here" rel="alternate" type="text/html"/>
    <title>Food Goes In Here</title>
<content type="html">
            There's an episode of The Simpsons where Homer's inner-child point's to his mouth saying &quot;Food goes in here.&quot; To which Homer replies &quot;It sure does.&quot;

&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;span&gt;Model&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&amp;lt;= Business logic goes in here&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;span&gt;View&lt;/span&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;span&gt;Controller&lt;/span&gt;&lt;/td&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

It sure does.

As a general rule I avoid blogging about the obvious, but sometimes what's obvious varies from person to person. I've run into the misconception that Rails does not support this obvious statement, that business logic goes into the Model. In Rails, according to this misconception, the Model is simply a Data Access Layer, and business logic winds up in the helpers or elsewhere. The purpose of this post is mainly to give me something to link to or copy and paste as the mood strikes me when I next run into this misconception.

In Ruby on Rails, as in any good MVC framework, business logic goes in the Model. All of it. Every shred of it.

I'm currently working on a project to develop a point-of-sale and online purchasing system with a great deal of very complex (some might argue overly complex) business logic. As a simpler example, displaying the available quantity of a specific item involves considering at which warehouse the order is being fulfilled, the quantities in various bins, and quantities from the bins which have already been allocated to an order but not yet pulled.

None of that business logic is in the Controller. None of it is in the View. And none of it is in a helper. It's all in the Model. The only thing the View needs to do is this:
&lt;pre class=&quot;ruby&quot;&gt;item.quantity_available&lt;/pre&gt;
I'm not sure where this notion that business logic goes in the helper comes from. For the record:
&lt;blockquote&gt;&lt;p&gt;A &lt;span&gt;helper&lt;/span&gt; is simply a module containing methods that assist a view. Helper methods are output-centric. They exist to generate HTML (or XML, or JavaScript) -- a helper extends the behavior of a template.&lt;/p&gt;
&lt;p&gt;-- &amp;lt;u&gt;Agile Web Development With Rails&amp;lt;/u&gt;, Dave Thomas, David Heinemeier Hansson
&lt;/blockquote&gt;Put another way, you can off-load code to the helper to avoid putting too much into your View templates.&lt;/p&gt;

For further reading on this topic, here's a few handy links:
&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model&quot;&gt;Skinny Controller, Fat Model&lt;/a&gt; by Jamis Buck&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://www.therailsway.com/2007/6/1/railsconf-recap-skinny-controllers&quot;&gt;RailsConf Recap: Skinny Controllers&lt;/a&gt; by Jamis Buck&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://blog.kineticweb.com/articles/2007/06/01/skinny-controller-fat-model&quot;&gt;Skinny Controller, Fat Model&lt;/a&gt; by Colin A. Bartlett
&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://blog.imperialdune.com/2007/4/19/find-methods-in-controllers&quot;&gt;Find methods in controllers&lt;/a&gt; by Graeme Nelson&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;http://blog.caboo.se/articles/2007/6/19/rspec-notes-from-the-trenches-2&quot;&gt;Rspec notes from the trenches-2&lt;/a&gt; by Courtenay over on Caboo.se &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;&lt;/h1&gt;
          </content>  </entry>
</feed>
