<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>NetManiac</title>
	
	<link>http://nhw.pl/wp</link>
	<description>Witold Rugowski on web20 wave with Ruby on Rails</description>
	<pubDate>Thu, 24 Sep 2009 14:49:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/Netmaniac" type="application/rss+xml" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">Netmaniac</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Extracting fixtures</title>
		<link>http://nhw.pl/wp/2009/09/24/extracting-fixtures</link>
		<comments>http://nhw.pl/wp/2009/09/24/extracting-fixtures#comments</comments>
		<pubDate>Thu, 24 Sep 2009 14:49:54 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[tdd]]></category>

		<category><![CDATA[test]]></category>

		<category><![CDATA[testing]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=482</guid>
		<description><![CDATA[I&#8217;m still using fixtures. Shame, I know.
Why I do use them instead of Factory Girl or other solution like that? Well, fixtures can be much more closer to real data than mocks from Factory. How come, You ask? Fixtures are imaginary data exactly like mocks from other sources!
My answer is: that depends how You create [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still using fixtures. Shame, I know.</p>
<p>Why I do use them instead of Factory Girl or other solution like that? Well, fixtures can be much more closer to real data than mocks from Factory. How come, You ask? Fixtures are <em>imaginary</em> data exactly like mocks from other sources!</p>
<p>My answer is: that depends how You create fixtures. If You create them by hand, indeed they are disconnected from real world (like all mocks). </p>
<div id="attachment_485" class="wp-caption aligncenter" style="width: 510px"><img src="http://nhw.pl/wp/wp-content/uploads/imaginary.jpg" alt="What is Your real data/mocks ratio?" title="imaginary" width="500" height="375" class="size-full wp-image-485" /><p class="wp-caption-text">What is Your real data/mocks ratio? CC by <a href='http://www.flickr.com/photos/hsing/'>hsing</a></p></div>
<p>But I prefer to extract fixtures from real (production) database. That way I can easily pick entries created by users which are edge cases. The only trouble is with creating fixtures. For some time I&#8217;m using modified <a href="http://nhw.pl/wp/2008/10/11/rake-and-arguments-for-tasks">extract_fixtures</a> rake task. I have added some conditions to extracting process - SQL condition to select only particular records and adjusted syntax to recent rake.</p>
<p>This is useful especially when You are about to take over application code which has no tests. Extracting real data is quick way to start write integration tests (in such case they have are most efficient - time invested and application code coverage).</p>
<h2>How to extract fixtures without pain?</h2>
<p><span id="more-482"></span><br />
Now I want to share with You next improvement to this recipe. Old fixtures can be left unchanged (as long You don&#8217;t use ERb to spice them up).</p>
<p>Rake task load old fixtures file and <strong>add</strong> new records as selected from database by arguments You are providing. More - it keeps old fixtures names so all Your tests using <code>some_table :fixture</code> will work (but don&#8217;t You dare to create attribute called <code>fixtures_old_key_value</code> :)) ). As a bonus attributes in Your fixture file will be sorted by attribute name!</p>
<p>OK some examples (written by hand not real YML files I hope there are no mistakes ;)) :</p>
<pre>
cat test/fixtures/entries.yml
one:
  attr1: value
  attr2: value
  created_at: 2009-09-10 11:22

rake extract_fixtures[entries,1,"created_at<'2009-09-01'"]
cat test/fixtures/entries.yml
one:
  attr1: value
  attr2: value
  created_at: 2009-09-10 11:22
  id: 1
entries_2:
  attr1: value from DB
  attr2: Also value from DB
  created_at: 2009-08-01 22:11
  id: 2
</pre>
<p>All is clear? In file there was fixture named <code>:one</code>, after rake task we have added new one selected from database, new fixture has name <em>TABLE_NAME_ID</em>, old fixture has unchanged name.</p>
<p>You can add DISCARD=true and old fixtures will be well&#8230; discarded :)</p>
<p>Rake task take following arguments:</p>
<ol>
<li>table name</li>
<li>limit number of records extracted from database</li>
<li>WHERE condition</li>
</ol>
<p>Rake has one limitation - it treats all commas as arguments separators, so it is not possible to use IN operator. Instead of <code>rake extract_fixtures[entries,10,"id IN (1,2,3)"]</code> write <code>rake extract_fixtures[entries,10,"id=1 OR id=2 OR id=3)"]</code>.</p>
<p>You can download rake task file: <a href="http://nhw.pl/download/extract_fixtures.rake">http://nhw.pl/download/extract_fixtures.rake</a></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=JtJAkKeZEco:uGL44fI7J4E:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=JtJAkKeZEco:uGL44fI7J4E:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=JtJAkKeZEco:uGL44fI7J4E:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=JtJAkKeZEco:uGL44fI7J4E:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=JtJAkKeZEco:uGL44fI7J4E:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=JtJAkKeZEco:uGL44fI7J4E:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=JtJAkKeZEco:uGL44fI7J4E:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/09/24/extracting-fixtures/feed</wfw:commentRss>
		</item>
		<item>
		<title>Logger - simple, yet powerful Rails debugging tool</title>
		<link>http://nhw.pl/wp/2009/09/15/logger-simple-yet-powerful-rails-debugging-tool</link>
		<comments>http://nhw.pl/wp/2009/09/15/logger-simple-yet-powerful-rails-debugging-tool#comments</comments>
		<pubDate>Tue, 15 Sep 2009 22:35:02 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=474</guid>
		<description><![CDATA[I don&#8217;t know about You, but logs are for me most powerful debugging tool. Placing many logger.debug or logger.info can quickly provide info what is happening inside Rails application. 
This approach is especially useful when something wrong is happening and trigger is unknown. Placing many logging directives can provide data for analysis what could be [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t know about You, but logs are for me most powerful debugging tool. Placing many <code>logger.debug</code> or <code>logger.info</code> can quickly provide info what is happening inside Rails application. </p>
<p>This approach is especially useful when something wrong is happening and trigger is unknown. Placing many logging directives can provide data for analysis what could be a reason.</p>
<div id="attachment_477" class="wp-caption aligncenter" style="width: 510px"><img src="http://nhw.pl/wp/wp-content/uploads/logbook.jpg" alt="Logbook" title="logbook" width="500" height="324" class="size-full wp-image-477" /><p class="wp-caption-text">CC by <a href='http://www.flickr.com/photos/addyeddy/'>Admond</a></p></div>
<p>Default Rails logger has one serious flaw which makes logs on production sites almost useless - messages are not grouped in calls. If You have many processes of Rails running and logging to single file, some requests will be processed in parallel and You have log entries mixed. With default log format there is no way to say which entry is from which process.</p>
<p>Since Rails 2.0 we have <code>ActiveSupport::BufferedLogger</code>, but it solves other problem - number of disk writes and file locks - You can set after how many entries log will be flushed to disk.</p>
<h3>AnnotatedLogger</h3>
<p>Here comes <code>AnnotatedLogger</code> for a rescue. The idea is to take each message and prefix it with PID of Rails process. As long You don&#8217;t run Rails in multithread mode, this is unique ID which will make log entry distinguishable.</p>
<pre name="code" class="ruby">
class AnnotatedLogger &lt; Logger

  def initialize *args
    super *args

    [:info, :debug, :warn, :error, :fatal].each {|m|
      AnnotatedLogger.class_eval %Q|
      def #{m} arg=nil, &#038;block
        pid = "%.5d:" % $$
        if block_given?
          arg = yield
        end
        super "%s %s" % [pid, arg.gsub(/\n/,"\n%s" % pid)]
      end
      |

    }
  end
end
</pre>
<p>Now in <code>Rails::Initializer.run do |config|</code> section of <code>config/environment.rb</code> define AnnotatedLogger as default Rails logger:</p>
<pre name="code" class="ruby">
 config.logger = AnnotatedLogger.new "log/#{RAILS_ENV}.log"
</pre>
<p>Of course You can add other data to log entries (timestamp?). Here is an example of log entries:</p>
<pre>
24551:Processing SearchController#processing (for [FILTERED] at 2009-09-15 15:11:24) [GET]
24551:   Session ID: df260892836fc619ec666f894e7d8e88
24551:   Parameters: {[FILTERED]}
24542:   Airport Load (0.216460)   SELECT * FROM [FILTERED]
24542: Completed in 0.24903 (4 reqs/sec) | Rendering: 0.01298 (5%) | DB: 0.22554 (90%) | 200 OK [FILTERED]
24551:   Search Columns (0.004711)   SHOW FIELDS FROM [FILTERED]
24551: Rendering template within layouts/blank
24551: Rendering search/processing
</pre>
<p>Without PIDs You would expect that <code>Airport Load</code> entry is part of <code>SearchController#processing</code> for session with ID <code>df260892836fc619ec666f894e7d8e88</code>. In reality this is output from processing different request. </p>
<h3>What else?</h3>
<p>This how I do deal with logs from Rails application. Do You have other ideas how to make logging more usable not only in development environment?</p>
<p><small><br />
<b>PS</b><br />
I have just other idea - probably You could use <code>BufferedLogger</code> with disabled auto flushing and patch <code>ActionController</code> to flush manually all entries after request was processed - then all messages would be dumped in single block.<br />
</small></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=OJ6UDn9lLPg:Lcca6vjAssk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=OJ6UDn9lLPg:Lcca6vjAssk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=OJ6UDn9lLPg:Lcca6vjAssk:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=OJ6UDn9lLPg:Lcca6vjAssk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=OJ6UDn9lLPg:Lcca6vjAssk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=OJ6UDn9lLPg:Lcca6vjAssk:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=OJ6UDn9lLPg:Lcca6vjAssk:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/09/15/logger-simple-yet-powerful-rails-debugging-tool/feed</wfw:commentRss>
		</item>
		<item>
		<title>Testing binary downloads with Webrat</title>
		<link>http://nhw.pl/wp/2009/09/09/testing-binary-downloads-with-webrat</link>
		<comments>http://nhw.pl/wp/2009/09/09/testing-binary-downloads-with-webrat#comments</comments>
		<pubDate>Wed, 09 Sep 2009 12:28:09 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[testing]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[webrat]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=466</guid>
		<description><![CDATA[I&#8217;m using Webrat to keep some sanity when approaching maintenance of new application. Customers often come to me with legacy code, which somehow is not covered by tests.
In such case integration tests are way to go, since they provide most bang of Yours bucks - each written test could cover many parts of application. 
I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using <a href="http://github.com/brynary/webrat/tree/master">Webrat</a> to keep some sanity when approaching maintenance of <em>new</em> application. Customers often come to me with legacy code, which somehow is not covered by tests.</p>
<p>In such case integration tests are way to go, since they provide most bang of Yours bucks - each written test could cover many parts of application. </p>
<p>I had to create test for testing download some data in CSV format (have You said binary? :) ). With default matchers from Webrat You won&#8217;t be able to write effective assertions - and that why I&#8217;m referring to such file as binary. </p>
<p><img src="http://nhw.pl/wp/wp-content/uploads/up-down-load.jpg" alt="up-down-load" title="up-down-load" width="500" height="333" class="aligncenter size-full wp-image-467" /></p>
<h2>So how to do it? Here is a quick tip</h2>
<p>Use Webrat&#8217;s <code>response_body</code> to get raw body returned by application. Like that:</p>
<pre name="code" class="ruby">
click_link "Get me some CSV data"
ret = CSV.parse response_body
assert_equal(
  2,
  ret[2][5].to_f,
  "In third row and sixth column You should have 2 and there is #{ret[2][5]}"
)
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=t9CoEPosOiA:tuZE7PkYDkI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=t9CoEPosOiA:tuZE7PkYDkI:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=t9CoEPosOiA:tuZE7PkYDkI:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=t9CoEPosOiA:tuZE7PkYDkI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=t9CoEPosOiA:tuZE7PkYDkI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=t9CoEPosOiA:tuZE7PkYDkI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=t9CoEPosOiA:tuZE7PkYDkI:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/09/09/testing-binary-downloads-with-webrat/feed</wfw:commentRss>
		</item>
		<item>
		<title>Are ActiveRecord validations worth anything?</title>
		<link>http://nhw.pl/wp/2009/07/20/are-activerecord-validations-worth-anything</link>
		<comments>http://nhw.pl/wp/2009/07/20/are-activerecord-validations-worth-anything#comments</comments>
		<pubDate>Mon, 20 Jul 2009 16:52:11 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[Security]]></category>

		<category><![CDATA[activerecord]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=461</guid>
		<description><![CDATA[ActiveRecord, which is core component of Rails framework (at least before Rails 3.0 become reality) provides a lot features which developers do love.
Validations are one of those features. They are methods which provide easy way to check if model is valid and protect consistency our data in database. Sounds good, but this is bullshit.
Active Record [...]]]></description>
			<content:encoded><![CDATA[<p>ActiveRecord, which is core component of Rails framework (at least before Rails 3.0 become reality) provides a lot features which developers do love.</p>
<p>Validations are one of those features. They are methods which provide easy way to check if model is valid and protect consistency our data in database. Sounds good, but this is bullshit.</p>
<div id="attachment_462" class="wp-caption aligncenter" style="width: 510px"><img src="http://nhw.pl/wp/wp-content/uploads/bullshit.jpg" alt="What AR really is ;) (c) CC <a href='http://www.flickr.com/photos/sekimura/'>sekimura</a>&#8221; title=&#8221;bullshit&#8221; width=&#8221;500&#8243; height=&#8221;375&#8243; class=&#8221;size-full wp-image-462&#8243; /><p class="wp-caption-text">What AR really is ;)  (c) CC <a href='http://www.flickr.com/photos/sekimura/'>sekimura</a></p></div>
<p><strong>Active Record validations are prone to race conditions</strong>. Period. It does not make any sense to rely on them if You <em>really</em> have to have consistent data (I&#8217;m referring to unique constraint and <code>validates_uniqueness_of</code>). The only way to go is to have constraints put on database level. Or write a lot workaround code in Rails. Error prone as well.</p>
<p>What is race condition? <a href="http://en.wikipedia.org/wiki/Race_condition">Race condition (or race hazard)</a> is when outcome of some operation depends on timing between other operations.</p>
<p>Let&#8217;s take for example creation of two records where one attribute should be unique. How does work <code>validates_uniqueness_of</code>?</p>
<p>First it checks in DB (via SELECT) if there already is record with such value as unique attribute. If there is no such record then it run INSERT command to create new record.</p>
<p>Now imagine two processes are trying to create such record in the same time. Since SELECT and INSERT are separate operation it is quite possible (remember we have two processes trying to do the same thing at once):</p>
<ol>
<li>Model.save in PROCESS 1</li>
<li>Model.save in PROCESS 2</li>
<li>SELECT FROM PROCESS 1 (result - no record in DB)</li>
<li>SELECT FROM PROCESS 2 (result - no record in DB)</li>
<li>INSERT FROM PROCESS 1</li>
<li>INSERT FROM PROCESS 1</li>
</ol>
<p>Now guess how many records will be created? :))</p>
<h2>What is takeaway from this rant?</h2>
<p>ActiveRecord brings to table a lot improvements which each developer loves, but there is no <em>silver bullet</em>. Such race condition can happen (unless You run one Rails process in non-threading mode, but this is not very useful setup :D) even on low traffic application.</p>
<p>If there is really some business need which requires You to have unique data You have to implement some constraints on database level. </p>
<p>Use AR, since it is wonderful tool, but when used properly. Or maybe - <strong>know shortcomings of tools You do use</strong>.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=Iz22pyRJgto:d2Q2qNsD9GM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=Iz22pyRJgto:d2Q2qNsD9GM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=Iz22pyRJgto:d2Q2qNsD9GM:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=Iz22pyRJgto:d2Q2qNsD9GM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=Iz22pyRJgto:d2Q2qNsD9GM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=Iz22pyRJgto:d2Q2qNsD9GM:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=Iz22pyRJgto:d2Q2qNsD9GM:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/07/20/are-activerecord-validations-worth-anything/feed</wfw:commentRss>
		</item>
		<item>
		<title>Paperclip, passenger and “not recognized by the ‘identify’ command”</title>
		<link>http://nhw.pl/wp/2009/07/14/paperclip-passenger-identify-not-recognized</link>
		<comments>http://nhw.pl/wp/2009/07/14/paperclip-passenger-identify-not-recognized#comments</comments>
		<pubDate>Tue, 14 Jul 2009 09:43:51 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[FreeBSD]]></category>

		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[paperclip]]></category>

		<category><![CDATA[passenger]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=458</guid>
		<description><![CDATA[If You do use awesome :) Paperclip library, and on application served by Passenger You get errors like:

Avatar /tmp/stream.1170.0 is not recognized by the 'identify' command.

most probably Passenger does not have setup environment and is missing a path. On FreeBSD identify is placed in /usr/local/bin and AFAIR this path is not included by default in [...]]]></description>
			<content:encoded><![CDATA[<p>If You do use awesome :) <a href="http://github.com/thoughtbot/paperclip/tree/master">Paperclip library</a>, and on application served by Passenger You get errors like:</p>
<pre>
Avatar /tmp/stream.1170.0 is not recognized by the 'identify' command.
</pre>
<p>most probably Passenger does not have setup environment and is missing a path. On FreeBSD <code>identify</code> is placed in <code>/usr/local/bin</code> and AFAIR this path is not included by default in PATH. As result Passenger can not find this utility.</p>
<p>You can try to setup environment for Passenger (Apache?) or just add in appropriate environment (in my case it was <code>production.rb</code>):</p>
<pre>
Paperclip.options[:command_path] = "/usr/local/bin"
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=16UAGp95RXQ:59dLlQiP21I:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=16UAGp95RXQ:59dLlQiP21I:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=16UAGp95RXQ:59dLlQiP21I:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=16UAGp95RXQ:59dLlQiP21I:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=16UAGp95RXQ:59dLlQiP21I:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=16UAGp95RXQ:59dLlQiP21I:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=16UAGp95RXQ:59dLlQiP21I:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/07/14/paperclip-passenger-identify-not-recognized/feed</wfw:commentRss>
		</item>
		<item>
		<title>Design patterns or SOLID design</title>
		<link>http://nhw.pl/wp/2009/06/16/design-patterns-or-solid-design</link>
		<comments>http://nhw.pl/wp/2009/06/16/design-patterns-or-solid-design#comments</comments>
		<pubDate>Tue, 16 Jun 2009 19:10:17 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[application design]]></category>

		<category><![CDATA[design patterns]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=454</guid>
		<description><![CDATA[I&#8217;m using Rails and Ruby over 3 years now, but with each month I know there is a lot new things to learn. Recently I&#8217;m trying to learn much more about design patterns, and how to use them.
If You are new to design patterns and want to know why it is worth to learn this [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using Rails and Ruby over 3 years now, but with each month I know there is a lot new things to learn. Recently I&#8217;m trying to learn much more about design patterns, and how to use them.</p>
<p>If You are new to design patterns and want to know why it is worth to learn this stuff I recommend You watching this session from this year&#8217;s GORUCO (GOtham RUby COnference):</p>
<p><embed src='http://goruco2009.confreaks.com/player.swf' height='326' width='550' allowscriptaccess='always' allowfullscreen='true' flashvars='image=images%2F30-may-2009-15-40-solid-object-oriented-design-sandi-metz-preview.png&#038;file=http%3A%2F%2Fgoruco2009.confreaks.com%2Fvideos%2F30-may-2009-15-40-solid-object-oriented-design-sandi-metz-small.mp4&#038;plugins=viral-1'/></p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=HtrCjHE19zo:kTcgIkwVlQ0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=HtrCjHE19zo:kTcgIkwVlQ0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=HtrCjHE19zo:kTcgIkwVlQ0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=HtrCjHE19zo:kTcgIkwVlQ0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=HtrCjHE19zo:kTcgIkwVlQ0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=HtrCjHE19zo:kTcgIkwVlQ0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=HtrCjHE19zo:kTcgIkwVlQ0:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/06/16/design-patterns-or-solid-design/feed</wfw:commentRss>
		</item>
		<item>
		<title>Associations and to_json on collections of objects</title>
		<link>http://nhw.pl/wp/2009/05/22/associations-and-to_json-on-collections-of-objects</link>
		<comments>http://nhw.pl/wp/2009/05/22/associations-and-to_json-on-collections-of-objects#comments</comments>
		<pubDate>Fri, 22 May 2009 09:11:38 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[Other]]></category>

		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[Tech]]></category>

		<category><![CDATA[hint]]></category>

		<category><![CDATA[JSON]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=444</guid>
		<description><![CDATA[ActiveRecord&#8217;s Serialization module does provide to_json method. It allows include associations when dumping to JSON, but it works only for single objects. If You want to run it against collection, returned by find :all You will get:

&#62;&#62; Model.all.to_json :include =&#62;:association
TypeError: wrong argument type Hash (expected Data)
	from (irb):13:in `to_json'
	from (irb):13

If You want to get in JSON [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://apidock.com/rails/ActiveRecord/Serialization/to_json">ActiveRecord&#8217;s Serialization</a> module does provide <code>to_json</code> method. It allows include associations when dumping to JSON, but it works only for single objects. If You want to run it against collection, returned by <code>find :all</code> You will get:</p>
<pre>
&gt;&gt; Model.all.to_json :include =&gt;:association
TypeError: wrong argument type Hash (expected Data)
	from (irb):13:in `to_json'
	from (irb):13
</pre>
<p>If You want to get in JSON collection with some associations You have to do it manually:</p>
<pre class="ruby" name="code">
    @companies = Company.all, :include =&gt; :address
    render :text =&gt; "[ %s ]" % @companies.collect{ |c|
      c.to_json(:include =&gt; :address)
    }.join(","),
      :content_type =&gt; 'application/json'
</pre>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=0g4W6VUe6VY:OTh3bvZL8QE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=0g4W6VUe6VY:OTh3bvZL8QE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=0g4W6VUe6VY:OTh3bvZL8QE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=0g4W6VUe6VY:OTh3bvZL8QE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=0g4W6VUe6VY:OTh3bvZL8QE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=0g4W6VUe6VY:OTh3bvZL8QE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=0g4W6VUe6VY:OTh3bvZL8QE:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/05/22/associations-and-to_json-on-collections-of-objects/feed</wfw:commentRss>
		</item>
		<item>
		<title>MySQL and ERROR 1114 (HY000): The table TABLE is full</title>
		<link>http://nhw.pl/wp/2009/04/26/mysql-and-error-1114-hy000-the-table-table-is-full</link>
		<comments>http://nhw.pl/wp/2009/04/26/mysql-and-error-1114-hy000-the-table-table-is-full#comments</comments>
		<pubDate>Sun, 26 Apr 2009 10:29:30 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[Tech]]></category>

		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=440</guid>
		<description><![CDATA[Recently I did some site maintenance and had to dump MySQL database and upload it again. I was surprised when during loading data back to database process have stopped with error:
ERROR 1114 (HY000) at line 1052: The table 'searches' is full

It was MySQL 5.0, without cluster configuration. Google lead me to answers related to cluster [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I did some site maintenance and had to dump MySQL database and upload it again. I was surprised when during loading data back to database process have stopped with error:</p>
<p><code>ERROR 1114 (HY000) at line 1052: The table 'searches' is full<br />
</code></p>
<p>It was MySQL 5.0, without cluster configuration. Google lead me to answers related to cluster setup, so I got quickly frustrated. It would be good for MySQL to provide some database of most common reasons for errors reported by MySQL.</p>
<p>This time reason was quite trivial - there was not enough space on file system to upload database. Why? I was changing MySQL setup to <code>innodb_file_per_table</code>. Since when doing such change You won&#8217;t get back space allocated to old database in file ibdata1 in mysql directory. Since database was not so small there was not enough space to create new database.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=FBkc0_eBEo8:vrJeheqv4-c:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=FBkc0_eBEo8:vrJeheqv4-c:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=FBkc0_eBEo8:vrJeheqv4-c:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=FBkc0_eBEo8:vrJeheqv4-c:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=FBkc0_eBEo8:vrJeheqv4-c:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=FBkc0_eBEo8:vrJeheqv4-c:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=FBkc0_eBEo8:vrJeheqv4-c:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/04/26/mysql-and-error-1114-hy000-the-table-table-is-full/feed</wfw:commentRss>
		</item>
		<item>
		<title>Test benchmark - useful gem for Rails tests</title>
		<link>http://nhw.pl/wp/2009/04/15/test-benchmark-useful-gem-for-rails-tests</link>
		<comments>http://nhw.pl/wp/2009/04/15/test-benchmark-useful-gem-for-rails-tests#comments</comments>
		<pubDate>Wed, 15 Apr 2009 21:59:52 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[Other]]></category>

		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[gems]]></category>

		<category><![CDATA[plugins]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[tdd]]></category>

		<category><![CDATA[tests]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=435</guid>
		<description><![CDATA[Doing TDD You need to know which tests are making whole suite slower. If time used by rake test becomes too long, You may be tempted to skip running tests&#8230; Waiting for them to complete becomes suddenly burden on Your way to next task.
So, when You need to optimize Your test suite, test_benchmark is a [...]]]></description>
			<content:encoded><![CDATA[<p>Doing TDD You need to know which tests are making whole suite slower. If time used by <code>rake test</code> becomes too long, You may be tempted to skip running tests&#8230; Waiting for them to complete becomes suddenly burden on Your way to next task.</p>
<div id="attachment_436" class="wp-caption aligncenter" style="width: 510px"><img src="http://nhw.pl/wp/wp-content/uploads/waiting.jpg" alt="Waiting... Waiting..." title="Waiting... Waiting..." width="500" height="402" class="size-full wp-image-436" /><p class="wp-caption-text">Waiting... Waiting... (c) <a href='http://www.flickr.com/photos/tonythemisfit/'>Tony the Misfit</a></p></div>
<p>So, when You need to optimize Your test suite, <a href="http://railspikes.com/2009/4/3/rails-test-benchmarks">test_benchmark</a> is a way to go. Just install Rails plugin and after <code>rake test</code> You will get 10 slowest tests and timing of all test in <code>test.log</code>. Now You know where to look for a good place to start and not to wait too long&#8230; </p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=wI5h_0r6bhI:DKqUeBvwo2I:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=wI5h_0r6bhI:DKqUeBvwo2I:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=wI5h_0r6bhI:DKqUeBvwo2I:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=wI5h_0r6bhI:DKqUeBvwo2I:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=wI5h_0r6bhI:DKqUeBvwo2I:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=wI5h_0r6bhI:DKqUeBvwo2I:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=wI5h_0r6bhI:DKqUeBvwo2I:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/04/15/test-benchmark-useful-gem-for-rails-tests/feed</wfw:commentRss>
		</item>
		<item>
		<title>MySQL, collations and Rails - or server, database, connection - that is not all</title>
		<link>http://nhw.pl/wp/2009/02/26/mysql-collations-and-rails-or-server-database-connection-that-is-not-all</link>
		<comments>http://nhw.pl/wp/2009/02/26/mysql-collations-and-rails-or-server-database-connection-that-is-not-all#comments</comments>
		<pubDate>Thu, 26 Feb 2009 21:32:38 +0000</pubDate>
		<dc:creator>Witold Rugowski</dc:creator>
		
		<category><![CDATA[RubyOnRails]]></category>

		<category><![CDATA[collation]]></category>

		<category><![CDATA[mysql]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[Ruby]]></category>

		<category><![CDATA[tips]]></category>

		<category><![CDATA[utf]]></category>

		<guid isPermaLink="false">http://nhw.pl/wp/?p=425</guid>
		<description><![CDATA[Last few weeks were somewhat crazy time. But I&#8217;m seeing finish line not so far away and I hope to find more time for blogging in upcoming weeks. At least I have few things to share, which could be interesting for other Rails developers.
So let&#8217;s start with MySQL and it&#8217;s collations. I have wrote about [...]]]></description>
			<content:encoded><![CDATA[<p>Last few weeks were somewhat crazy time. But I&#8217;m seeing finish line not so far away and I hope to find more time for blogging in upcoming weeks. At least I have few things to share, which could be interesting for other Rails developers.</p>
<p>So let&#8217;s start with MySQL and it&#8217;s collations. <a href="http://nhw.pl/wp/2008/09/16/mysql-collate-setting-in-rails-application">I have wrote about them few months ago</a>. Few days ago, I had finally installed Linux on my notebook (last two months I was running Linux from USB drive). And it seems that there were some minor changes in my environment. I was working on <a href="http://cartoteka.pl/">CARtoteka</a>, and when I was running migrations they were stopped in half with error like :</p>
<pre>
Mysql::Error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and
(utf8_general_ci,COERCIBLE) for operation '=':
SELECT count(DISTINCT  companies`.id) AS count_all FROM `companies`
LEFT OUTER JOIN `addresses` ON  addresses.company_id = companies.id
WHERE (city = 'Poznań' and moderated)
</pre>
<p>What the heck? I have doublechecked I have set connection collation and other - <code>utf_general_ci</code> as I could expect.</p>
<p>After some digging I have found what was a cause.</p>
<p><span id="more-425"></span><br />
MySQL support different collations for each table and even for each column in tables (of course only for text types). But this can lead to problems with defaults. </p>
<p>When Rails does create new tables and sets character set as UTF8, it does not select default collation for table. That means MySQL will use connection collation, and if it was not set by You then huge chance that it is again <a href="http://search.yahoo.com/search?p=swedish+chef&#038;vc=&#038;fr=yfp-t-501&#038;toggle=1&#038;cop=mss&#038;ei=UTF-8">unforgettable Swedish Chef</a> or <code>latin1_swedish_ci</code> in other words.</p>
<h2>What to do?</h2>
<p>If You can fall back to previous versions You can create tables with new defaults.</p>
<p>To make sure You are not depended on connection settings You just provide collation information when creating table. Just add </code>:options</code> to <code>create_table</code> specifying UTF8 as character set and <code>utf_general_ci</code> as default collation for tables :</p>
<pre class='ruby' name='code'>
create_table :tags,
   :options => "DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci" do |t|
      t.column :name, :string
end
</pre>
<p>If it is not possible, then You have to use SQL and ALTER TABLE.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Netmaniac?a=BzqukgCXIKw:V4cc5A8Oca0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Netmaniac?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=BzqukgCXIKw:V4cc5A8Oca0:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=BzqukgCXIKw:V4cc5A8Oca0:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=BzqukgCXIKw:V4cc5A8Oca0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=BzqukgCXIKw:V4cc5A8Oca0:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Netmaniac?a=BzqukgCXIKw:V4cc5A8Oca0:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Netmaniac?i=BzqukgCXIKw:V4cc5A8Oca0:gIN9vFwOqvQ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://nhw.pl/wp/2009/02/26/mysql-collations-and-rails-or-server-database-connection-that-is-not-all/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
