<?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>jonathanjulian.com &#187; Ruby</title>
	<atom:link href="http://jonathanjulian.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanjulian.com</link>
	<description>Ruby, Rails, JavaScript, software development</description>
	<lastBuildDate>Wed, 30 Jun 2010 21:40:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rails to_json or as_json?</title>
		<link>http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/</link>
		<comments>http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 17:08:36 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=187</guid>
		<description><![CDATA[A really great modification was introduced in Rails 2.3.3 &#8211; and while everyone clamored about JSON encoding speeds and C vs Ruby implementations, the blogosphere overlooked the clean separation of responsibility that was introduced.

In the &#8220;old days&#8221;, you&#8217;d override to_json in your model class to provide a JSON implementation of your model. Then in your [...]]]></description>
			<content:encoded><![CDATA[<p>A really great modification was introduced in <a href="http://weblog.rubyonrails.org/2009/7/20/rails-2-3-3-touching-faster-json-bug-fixes">Rails 2.3.3</a> &#8211; and while everyone clamored about JSON <a href="http://www.ruby-forum.com/topic/200017">encoding speeds</a> and <a href="http://flori.github.com/json/">C vs Ruby implementations</a>, the blogosphere overlooked the clean separation of responsibility that was introduced.</p>

<p>In the &#8220;old days&#8221;, you&#8217;d override <code>to_json</code> in your model class to provide a JSON implementation of your model. Then in your controller, <code>render :json =&gt; @model</code> would work perfectly. And some folks would even redundantly code <code>render :json =&gt; @model.to_json</code>, and that would work too.</p>

<p><code>to_json</code> even had some great options for ActiveRecord objects! You could tell the method to only render certain attributes, or to include associations or method calls!</p>

<pre><code>render :json =&gt; 
  @user.to_json(:only =&gt; [:email], :include =&gt; [:addresses])
</code></pre>

<p>Life was good. But things start to fall apart when you want to do something a little out of the ordinary. Like return JSON with the model as part of a bigger structure.</p>

<pre><code>render :json =&gt; { :success =&gt; true, 
  :user =&gt; @user.to_json(:only =&gt; [:email]) }
</code></pre>

<p>Oops. <code>{\"user\":{\"email\":\"me@example.com\","success":true}</code>has the JSON characters <em>escaped</em>, which is not what we want. So what do we do? We hack around it:</p>

<pre><code>render :json =&gt; { :success =&gt; true, 
  :user =&gt; { :email =&gt; @user.email } }
</code></pre>

<p>But this doesn&#8217;t scale &#8211; we have to explicitly create the JSON <em>by hand</em> in the <em>controller</em>. What if we need 5 or more attributes? Yuck!</p>

<p>Enter <strong>ActiveSupport 2.3.3</strong>. Now the <em>creation</em> of the json is separate from the <em>rendering</em> of the json. <code>as_json</code> is used to create the structure of the JSON as a Hash, and the rendering of that hash into a JSON string is left up to <a href="http://as.rubyonrails.org/classes/ActiveSupport/JSON.html"><code>ActiveSupport::json.encode</code></a>. You should never use <code>to_json</code> to <em>create</em> a representation, only to <em>consume</em> the representation.</p>

<pre><code>def as_json(options={})
  { :email =&gt; self.email }
end
</code></pre>

<p>Anytime <code>to_json</code> is called on an object, <code>as_json</code> is invoked to create the data structure, and then that hash is encoded as a JSON string using <code>ActiveSupport::json.encode</code>. This happens for all types: Object, Numeric, Date, String, etc (see <a href="http://github.com/rails/rails/tree/2-3-stable/activesupport/lib/active_support/json">active_support/json</a>).</p>

<p>ActiveRecord objects behave the same way. There is a default <code>as_json</code> <a href="http://github.com/rails/rails/blob/2-3-stable/activerecord/lib/active_record/serializers/json_serializer.rb">implementation</a> that creates a Hash that includes all the model&#8217;s attributes. <strong>You should override <code>as_json</code> in your Model to create the JSON structure you want</strong>. <code>as_json</code>, just like the old <code>to_json</code>, takes an option hash where you can specify attributes and methods to include declaratively.</p>

<pre><code>def as_json(options={})
  super(:only =&gt; [:email, :avatar], :include =&gt;[:addresses])
end
</code></pre>

<p>Your controller code to display one model should always look like this:</p>

<pre><code>render :json =&gt; @user
</code></pre>

<p>And if you have to do anything <a href="http://namxam.tumblr.com/post/396486333/rails-as-json-vs-to-json">out of the ordinary</a>, call <code>as_json</code> passing your options.</p>

<pre><code>render :json =&gt; { :success =&gt; true, 
  :user =&gt; @user.as_json(:only =&gt; [:email]) }
</code></pre>

<p>The moral of the story is: <strong>In controllers, do not call <code>to_json</code> directly, allow <code>render</code> to do that for you. If you need to tweak the JSON output, override <code>as_json</code> in your model, or call <code>as_json</code> directly.</strong></p>

<p>Fix your code now to use <code>as_json</code> &#8211; it will be one less thing to worry about when you migrate to Rails 3.</p>

<p><em>This post was inspired by the investigation I went into while exploring the answer to <a href="http://stackoverflow.com/questions/2572284/override-to-json-in-rails-2-3-5/2574900">this question on Stack Overflow</a>.</em></p>

<p><em>Hey Brian Morearty &#8211; Rails was never Javaficated to begin with. So the answer is <a href="http://bmorearty.wordpress.com/2009/07/20/to_json-as_json/">yes</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2010/04/rails-to_json-or-as_json/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Introduction to delayed_job</title>
		<link>http://jonathanjulian.com/2009/11/introduction-to-delayed_job/</link>
		<comments>http://jonathanjulian.com/2009/11/introduction-to-delayed_job/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 13:03:58 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=119</guid>
		<description><![CDATA[I gave this short presentation at the November bmore-on-rails meeting last night. Flip Sasser also presented an overview of Resque, and Michael Dotterer showed us a bit about background_job.

We decided that delayed_job is an easy and solid way to get background tasks running in your Rails app, but if you are a massive site that needs [...]]]></description>
			<content:encoded><![CDATA[<p>I gave this short presentation at the <a href="http://www.meetup.com/bmore-on-rails/calendar/11620904/">November</a> <a href="http://www.meetup.com/bmore-on-rails/">bmore-on-rails</a> meeting last night. <a href="http://twitter.com/flipsasser">Flip Sasser</a> also presented an overview of <a href="http://github.com/blog/542-introducing-resque">Resque</a>, and <a href="http://www.workingwithrails.com/person/17379-michael-dotterer">Michael Dotterer</a> showed us a bit about <a href="http://codeforpeople.rubyforge.org/svn/bj/trunk/README">background_job</a>.</p>

<p>We decided that delayed_job is an easy and solid way to get background tasks running in your Rails app, but if you are a massive site that needs a heavy-duty and configurable solution, then Resque may be what you need. Resque&#8217;s complexity makes this a non-trivial decision! Start with delayed_job since it&#8217;s so quick to implement, and you can always choose another solution later.</p>

<div id="__ss_2467230" style="width: 425px; text-align: left;"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" title="Introduction To Delayed Job" href="http://www.slideshare.net/jonathanjulian/introduction-to-delayed-job">Introduction To Delayed Job</a><object style="margin:0px" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introtodelayedjob-091110110012-phpapp01&amp;stripped_title=introduction-to-delayed-job" /><param name="allowfullscreen" value="true" /><embed style="margin:0px" type="application/x-shockwave-flash" width="425" height="355" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introtodelayedjob-091110110012-phpapp01&amp;stripped_title=introduction-to-delayed-job" allowscriptaccess="always" allowfullscreen="true"></embed></object>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">documents</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/jonathanjulian">Jonathan Julian</a>.</div>
</div>

<p>Want to get delayed_job running in your app in 10 minutes? Follow the steps in <a href="http://railstips.org/2008/11/19/delayed-gratification-with-rails">Delayed Gratification with Rails</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2009/11/introduction-to-delayed_job/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Wrapping a div around will_paginate page_entries_info</title>
		<link>http://jonathanjulian.com/2009/11/wrapping-a-div-around-will_paginate-page_entries_info/</link>
		<comments>http://jonathanjulian.com/2009/11/wrapping-a-div-around-will_paginate-page_entries_info/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 20:19:26 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=116</guid>
		<description><![CDATA[The page_entries_info view helper looks great, but it is just plain text; I can&#8217;t add margin or padding or float it. Here&#8217;s a quick alias_method to wrap it up in a div with the class of your choice.



Just drop that into an file in config/initializers, restart, and get styling!
]]></description>
			<content:encoded><![CDATA[<p>The page_entries_info view helper looks great, but it is just plain text; I can&#8217;t add margin or padding or float it. Here&#8217;s a quick alias_method to wrap it up in a div with the class of your choice.</p>

<script src="http://gist.github.com/230245.js"></script>

<p>Just drop that into an file in config/initializers, restart, and get styling!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2009/11/wrapping-a-div-around-will_paginate-page_entries_info/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I just entered RPCFN #2</title>
		<link>http://jonathanjulian.com/2009/10/i-just-entered-rpcfn-2/</link>
		<comments>http://jonathanjulian.com/2009/10/i-just-entered-rpcfn-2/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 19:58:20 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=110</guid>
		<description><![CDATA[I don&#8217;t consider myself a Ruby newbie, but I was drawn to the Ruby Programming Challenge For Newbies #2 today. I&#8217;ll post and explain my solution when the contest is over! Thanks to Chris Strom for submitting the challenge!
]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t consider myself a Ruby newbie, but I was drawn to the <a href="http://rubylearning.com/blog/2009/10/08/rpcfn-average-arrival-time-for-a-flight-2/">Ruby Programming Challenge For Newbies #2</a> today. I&#8217;ll post and explain my solution when the contest is over! Thanks to <a href="http://japhr.blogspot.com/">Chris Strom</a> for <a href="http://twitter.com/eee_c/status/4705290596">submitting</a> the challenge!</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2009/10/i-just-entered-rpcfn-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tweeting from RubyNation</title>
		<link>http://jonathanjulian.com/2009/06/tweeting-from-rubynation/</link>
		<comments>http://jonathanjulian.com/2009/06/tweeting-from-rubynation/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 15:09:38 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://jonathanjulian.com/?p=58</guid>
		<description><![CDATA[I&#8217;m posting updates from RubyNation 2009 on Twitter. Follow along with this search.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m posting updates from <a href="http://rubynation.org">RubyNation</a> 2009 on Twitter. Follow along with <a href="http://search.twitter.com/search?q=rubynation+jonathanjulian">this search</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonathanjulian.com/2009/06/tweeting-from-rubynation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
