<?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>Xor News &#187; Rails</title>
	<atom:link href="http://www.flester.com/blog/category/computers/rails/feed" rel="self" type="application/rss+xml" />
	<link>http://www.flester.com/blog</link>
	<description>You can't have it both ways</description>
	<lastBuildDate>Sat, 30 Jul 2011 12:18:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Scaling Rails Screencast</title>
		<link>http://www.flester.com/blog/2009/06/13/scaling-rails-screencast</link>
		<comments>http://www.flester.com/blog/2009/06/13/scaling-rails-screencast#comments</comments>
		<pubDate>Sat, 13 Jun 2009 16:38:41 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/2009/06/13/scaling-rails-screencast</guid>
		<description><![CDATA[This screen cast covers Scaling Rails apps using Rack and Metal and is an execellent tutorial on both subjects. Jason Pollack, one of the Rails Envy guys, does a superb job explaining how rack and metal work in Rails 2.3.]]></description>
			<content:encoded><![CDATA[<p>This screen cast covers <a href="http://nr-content.s3.amazonaws.com/railslab/videos/14-ScalingRails-Rack-and-Metal.mp4">Scaling Rails apps using Rack and Metal</a> and is an execellent tutorial on both subjects. Jason Pollack, one of the Rails Envy guys, does a superb job explaining how rack and metal work in Rails 2.3. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2009/06/13/scaling-rails-screencast/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://nr-content.s3.amazonaws.com/railslab/videos/14-ScalingRails-Rack-and-Metal.mp4" length="49733893" type="video/mp4" />
		</item>
		<item>
		<title>Using thrift from ant</title>
		<link>http://www.flester.com/blog/2009/04/26/using-thrift-from-ant</link>
		<comments>http://www.flester.com/blog/2009/04/26/using-thrift-from-ant#comments</comments>
		<pubDate>Sun, 26 Apr 2009 20:13:31 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Food]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/?p=139</guid>
		<description><![CDATA[We are starting to use thrift and needed an ant build recipe. Here&#8217;s what we came up with. It works good and the only thing that looks like an abstraction leakage to me is that I needed to know the package, the java namespace, for the resultant generated thrift code and the name of one [...]]]></description>
			<content:encoded><![CDATA[<p>We are starting to use <a href="http://incubator.apache.org/thrift/">thrift</a> and needed an ant build recipe. Here&#8217;s what we came up with. It works good and the only thing that looks like an abstraction leakage to me is that I needed to know the package, the java namespace, for the resultant generated thrift code and the name of one of the thrift generated files. The primary goal was to eliminate running the thrift generator when the generated code is newer than the .thrift files. There isn&#8217;t a one to one mapping between .thrift files and generated output so if any of the generated stuff is newer than any of the thrift then it all gets recreated.</p>
<p>I also didn&#8217;t want to have to copy the thrift output to someplace else, so a javac target was added to just treat the &#8220;gen-java&#8221; thrift output as a new source directory for direct java compilation. The normal ant target to compile the java code can now just depend on &#8220;thrift-gen&#8221;.</p>
<pre>
<textarea name="code" class="xml">
  &lt;property name="thrift.dir" value="${basedir}/src/thrift"/&gt;
  &lt;property name="thrift.out.dir" value="${basedir}/src/gen-java"/&gt;
  &lt;property name="thrift.package" value="your/package/name"/&gt;

  &lt;!-- thrift files --&gt;
  &lt;fileset id="thrift.files" dir="${thrift.dir}"&gt;
    &lt;include name="**/*.thrift"/&gt;
  &lt;/fileset&gt;
  &lt;!-- Determint if thrift generated java is up to date --&gt;
  &lt;target name="thrift-init"&gt;
    &lt;dependset&gt;
      &lt;srcfileset refid="thrift.files"/&gt;
      &lt;targetfileset dir = "${thrift.out.dir}/${thrift.package}"&gt;
        &lt;include name="**/*.java"/&gt;
      &lt;/targetfileset&gt;
    &lt;/dependset&gt;
    &lt;available file="${thrift.out.dir}/${thrift.package}/Foo.java"
               property="thrift.uptodate"/&gt;
  &lt;/target&gt;

  &lt;!-- Generate thrift java --&gt;
  &lt;target name="thrift-gen" depends="thrift-init" unless="thrift.uptodate"&gt;
    &lt;pathconvert property="thrift.file.list" refid="thrift.files"
               pathsep=" " dirsep="/"&gt;
    &lt;/pathconvert&gt;
    &lt;echo message="Running thrift generator on ${thrift.file.list}"/&gt;
    &lt;exec executable="thrift" dir="${basedir}" failonerror="true"&gt;
      &lt;arg line="--strict --gen java -o src ${thrift.file.list}"/&gt;
    &lt;/exec&gt;
  &lt;/target&gt;
</textarea></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2009/04/26/using-thrift-from-ant/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recent Rails Work</title>
		<link>http://www.flester.com/blog/2008/09/30/recent-rails-work</link>
		<comments>http://www.flester.com/blog/2008/09/30/recent-rails-work#comments</comments>
		<pubDate>Tue, 30 Sep 2008 01:46:38 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/?p=69</guid>
		<description><![CDATA[The big project our team has been working on for the last couple of months if finally public. It&#8217;s a fairly substantial rails app, about 40 models, lots of cool stuff. Most of the site is subscription based but you could checkout some of the info and look at the home page: http://globalsports.net/ It&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>The big project our team has been working on for the last couple of months if finally public. It&#8217;s a fairly substantial rails app, about 40 models, lots of cool stuff. Most of the site is subscription based but you could checkout some of the info and look at the home page:</p>
<p>   <a href="http://globalsports.net/">http://globalsports.net/</a></p>
<p>It&#8217;s a social networking site centered around sports videos and young athletes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2008/09/30/recent-rails-work/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails controller inheritance and before_filter problems</title>
		<link>http://www.flester.com/blog/2008/08/31/rails-controller-inheritance-and-before_filter-problems</link>
		<comments>http://www.flester.com/blog/2008/08/31/rails-controller-inheritance-and-before_filter-problems#comments</comments>
		<pubDate>Sun, 31 Aug 2008 02:15:32 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails controllers]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/?p=66</guid>
		<description><![CDATA[Rails controllers are just super nice. My MVC experience predates STRUTS by several years. So having done it by hand several times, and seen pretty much everything out there, I just find that Rails has a lot of nice features that make it super convenient to use: controller inheritance, before_filter, default routes, and much more. [...]]]></description>
			<content:encoded><![CDATA[<p>Rails controllers are just super nice. My MVC experience predates STRUTS by several years. So having done it by hand several times, and seen pretty much everything out there, I just find that Rails has a lot of nice features that make it super convenient to use: controller inheritance, before_filter, default routes, and much more.</p>
<p>But this week I got in a real jam with security.</p>
<p>I am inheriting a bunch of controllers from a plugin. All of the plugin controllers extend BaseController, which the plugin also supplies. In order to add or change some functions, I just create a controller by the same name, do what I want, extend BaseController and everything just works great.</p>
<p>Except when the controller in the plugin has a line like this:</p>
<p>  before_filter :login_required, :except => [:edit, :destroy]</p>
<p>If in my version of the controller I need to change it so that the edit and destroy actions are also protected by the login_required filter, well that gets tricky real fast. If you think you can just add</p>
<p>  before_filter :login_required</p>
<p>and you are covered, think again. If you want to try</p>
<p>  before_filter :login_required, :only => [:edit, :destroy]</p>
<p>well that doesn&#8217;t work either. Some people have a proposed (with diff and tests no less) that an :exclude and :include tag. This seems sensible and would be a solution to the problem I had.</p>
<p>What I chose to do is to wrap the filter I wanted to apply more broadly in a new name.</p>
<pre name="code" class="ruby">
class BaseController < ApplicationController
  before_filter :my_login_required

  protected

  def my_login_required
    login_required
  end

end
</pre>
<p>What that does is allow me to use my filter without regard to where it might collide with specifications from the inherited controllers. This makes it much easier to audit security in my application, is much less verbose, and pretty easy to understand.</p>
<p>In this case the filter that I wrapped was pretty lightweight. It didn't do any database lookups for normal cases. That's good because rails now doesn't know that login_required and my_login_required are really the same filter. So I'm paying the overhead of running it twice when I require my_login_required and the super class uses login_required. If it was a heavyweight method that would have caused a problem I would have to hack a little more to make sure that one of them could bail out early.</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2008/08/31/rails-controller-inheritance-and-before_filter-problems/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails can serve XML to flex based apps</title>
		<link>http://www.flester.com/blog/2008/08/26/rails-can-serve-xml-to-flex-based-apps</link>
		<comments>http://www.flester.com/blog/2008/08/26/rails-can-serve-xml-to-flex-based-apps#comments</comments>
		<pubDate>Tue, 26 Aug 2008 20:22:08 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/?p=64</guid>
		<description><![CDATA[On a recent project we served up a flash/flex based client from a Rails app. The flash client needed to call back into the app to retrieve xml to populate some displays. Nice. Rails is good that way, especially if you have started from scaffolded resources which have all the format.xml responders already in them. [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent project we served up a flash/flex based client from a Rails app. The flash client needed to call back into the app to retrieve xml to populate some displays. Nice. Rails is good that way, especially if you have started from scaffolded resources which have all the format.xml responders already in them.</p>
<p>Unfortunately the dashes that rails ActiveSupport::CoreExtensions::Array::Conversions uses<br />
(same goes for AS::CE::Hash::) takes the model attribute names and puts dashes into them<br />
to create the xml tags.</p>
<p>So if you have a User model with user_name attribute and you just want to return<br />
@user.to_xml, you are going to get </p>
<pre name="code" class="xml">
   &lt;user>
      &lt;user-name>value of name&lt;/user-name>
   &lt;/user>
</pre>
<p>Which looks pretty. But apparently the xml parser in flex can&#8217;t handle that. I couldn&#8217;t find any reference in the documentation on how to fix this, but when in doubt <a href="http://www.catb.org/jargon/html/U/UTSL.html">UTSL</a>, baby. Which shows that there is a :dasherize option that can be provided.</p>
<pre name="code" class="ruby">
  @user = User.find(params[:id])
  @user.to_xml(:dasherize => false)
</pre>
<p>Now the generated xml will have underscores instead of dashes. Flex developers everywhere should send <a href="http://loudthinking.com/">DHH</a> a nickel for that one. Ok, I don&#8217;t know who made the actual commit for it, but you&#8217;s should give David the nickel anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2008/08/26/rails-can-serve-xml-to-flex-based-apps/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Aliasing a Ruby class</title>
		<link>http://www.flester.com/blog/2008/08/12/aliasing-a-ruby-class</link>
		<comments>http://www.flester.com/blog/2008/08/12/aliasing-a-ruby-class#comments</comments>
		<pubDate>Tue, 12 Aug 2008 13:27:43 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/?p=63</guid>
		<description><![CDATA[In a Rails system with a pretty standard user model, views, and controller, I recently found that the users_controller was getting pretty large. There was a pretty significant amount of code that I wanted to add to deal with the users using a different set of views under some circumstances &#8212; and I didn&#8217;t want [...]]]></description>
			<content:encoded><![CDATA[<p>In a Rails system with a pretty standard user model, views, and controller, I recently found that the users_controller was getting pretty large. There was a pretty significant amount of code that I wanted to add to deal with the users using a different set of views under some circumstances &#8212; and I didn&#8217;t want to add all of that to the users_controller.rb.  What I wanted was to move these functions to a staff_controller.rb, use restful routing on it and just treat the user object differently.</p>
<p>This didn&#8217;t work out so well initially. Trying to use form_for in a view always reflects on the type of object you have and sends the results right back to the users_controller &#8212; not what I wanted. I tried a bunch of stuff to force the form_for to do what I wanted but no joy. What I wanted I thought was a way to alias the user model.</p>
<pre name="code" class="ruby">
class Staff < User
end
</pre>
<p>Problem solved.</p>
<p>The User model had some nice named scopes for finders, for example:</p>
</pre>
<pre name="code" class="ruby">
  named_scope
          :recent,
          :conditions => ["created_at > ?", 2.weeks.ago]
</pre>
<p>And now I can do all of the following</p>
<pre name="code" class="ruby">
  Staff.first
  Staff.recent
  Staff.find(params[:id])
  @staff = Staff.recent.find(:all,
              :conditions => ['name = ?',"jim"])
  @staff.last_login_at = Time.now
  @staff.save!
</pre>
<p>.. which since reflection now says they are staff objects, causes<br />
all the view helpers that reflect on stuff to work properly and doesn&#8217;t<br />
break any of the ActiveRecord stuff.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2008/08/12/aliasing-a-ruby-class/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using model objects in migrations is fragile</title>
		<link>http://www.flester.com/blog/2008/08/05/using-model-objects-in-migrations-is-fragile</link>
		<comments>http://www.flester.com/blog/2008/08/05/using-model-objects-in-migrations-is-fragile#comments</comments>
		<pubDate>Tue, 05 Aug 2008 02:07:37 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/?p=59</guid>
		<description><![CDATA[I got sucked into a trap. In a Rails migration it&#8217;s so easy to use an ActiveRecord::Base model object to update some stuff, and it looks nice and DRY. But it is extremely fragile. For example suppose your domain has a user and sponsor model. Each user has a sponsor. You start out this way: [...]]]></description>
			<content:encoded><![CDATA[<p>I got sucked into a trap. In a Rails migration it&#8217;s so easy to use an ActiveRecord::Base model object to update some stuff, and it looks nice and DRY. But it is extremely fragile. For example suppose your domain has a user and sponsor model. Each user has a sponsor. You start out this way:</p>
<pre name="code" class="ruby">
   def self.up
      create table :users do |t|
         t.string :name
         t.string :password
         t.string :sponsor_name
         t.timestamps
      end
   end
</pre>
<p>Later you want to insist that every user in the system have a sponsor. You already have a bunch of test data that you are intellectually invested in, so you whip up a quick migration to make sure.</p>
<pre name="code" class="ruby">
  def self.up
     User.find(:all, :conditions => 'sponsor_name IS NULL').each do |u|
         u.sponsor_name = "The Default"
         u.save!
      end
  end
</pre>
<p>Cool. Works good. You can go up and down. But some time later you realize the supreme error of your ways. You should have had a sponsor model and the user should have a sponsor_id, not a sponsor_name. Doh! What was I thinking. Time for another migration:</p>
<pre name="code" class="ruby">
   def self.up
      add_column :users, :sponsor_id, :integer
      drop_column :users, :sponsor_name
   end
</pre>
<p>and the corresponding self.down method too, and the necessary belongs_to, has_one in the user and sponsor model classes. But now you can&#8217;t migrate the database up from bootstrap any more because your use of the the User model with u.sponsor_name above will not work. The user no longer has a sponsor_name.</p>
<p>The root of the issue is that the model objects are not versioned along with the database as it goes up and down. Since fixtures just use the same model objects that doesn&#8217;t help one bit.</p>
<p>The only reliable way we have come up with is to inject data using raw sql. So step two above<br />
could change to</p>
<pre name="code" class="ruby">
  def self.up
     execute 'update users set sponsor_name = "The Default" where sponsor_name IS NULL'
  end
</pre>
<p>That works, up and down the line, because the database sql IS in sync with the database at every step.</p>
<p>Have a better way to write less brittle migrations? Let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2008/08/05/using-model-objects-in-migrations-is-fragile/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails Flash with Ajax</title>
		<link>http://www.flester.com/blog/2008/08/01/rails-flash-with-ajax</link>
		<comments>http://www.flester.com/blog/2008/08/01/rails-flash-with-ajax#comments</comments>
		<pubDate>Fri, 01 Aug 2008 03:38:00 +0000</pubDate>
		<dc:creator>mike</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.flester.com/blog/?p=58</guid>
		<description><![CDATA[The typical Ruby on Rails flash area is only drawn when there is a message to flash. I think something like this is fairly common (this example is haml, but that&#8217;s not the point) usually done in a partial, something like app/views/shared/_flash.html.haml: - %w(info notice error).each do &#124;type&#124; - if flash[type.to_sym] .warning.flash_message{:class => "#{type.to_s}"} %span= [...]]]></description>
			<content:encoded><![CDATA[<p>The typical Ruby on Rails flash area is only drawn when there is a message to flash. I think something like this is fairly common (this example is haml, but that&#8217;s not the point) usually done in a partial, something like app/views/shared/_flash.html.haml:</p>
<pre name="code" class="ruby">
- %w(info notice error).each do |type|
  - if flash[type.to_sym]
    .warning.flash_message{:class => "#{type.to_s}"}
      %span= h(flash[type.to_sym])
      %small
        %a{:href=>"#", :onclick=>"$$('div.flash_message')[0].hide();return false"} Close this message
 </pre>
<p>The problem with this is that if you are Ajaxing a bunch of stuff you could use the flash area for user interaction and highlighting messages, but if it isn&#8217;t there, that could be a problem. So how about doing this instead so that the flash area is always drawn, just not displayed:</p>
<pre name="code" class="ruby">
- %w(info notice error).each do |type|
  .warning.flash_message{ :class => "#{type}", :id => "flash_#{type}", :style => "display: #{flash[type.to_sym] ? 'block' : 'none'}" }
    %span
      - if flash[type.to_sym]
        = h(flash[type.to_sym])
    %small
      %a{:href=>"#", :onclick=>"$$('div.#{type.to_s}')[0].hide();return false"} Close this message
</pre>
<p>The flash closer is still in there so we don&#8217;t have to worry about the complexity of building that again every time, and the message is in a separate span element so it is easy to fill in. So then we can add a little bitty application helper (app/helpers/application_helper.rb)</p>
<pre name="code" class="ruby">
  def flashnow(page,msg)
    page.select("#flash_notice span").first.replace("<span>#{msg}</span>")
    page.select("#flash_notice").first.show
  end
</pre>
<p>You might need to change the replace method for replaceHtml<br />
depending on the version of prototype you are working with.<br />
and call it from any view that is building an rjs response:</p>
<pre name="code" class="ruby">
  flashnow(page,'Yeeha, cowboy, it worked!')
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.flester.com/blog/2008/08/01/rails-flash-with-ajax/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

