<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>a timocracy of one</title>
    <link>http://www.timocracy.com</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Tim Connor's Blog</description>
    <item>
      <title>named_scope dependencies via returning anonymous scopes from class methods</title>
      <description>&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; This doesn&amp;#8217;t actually quite work, as it can override some scopes when composed.  I haven&amp;#8217;t quite figured out yet, if the technique is salvageable in some situations, or if it is always unsafe to use for chaining scopes.&lt;/p&gt;


	&lt;p&gt;I have not been able to find any info on ways to make &lt;a href="http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality"&gt;rails named_scopes&lt;/a&gt; call each other, or otherwise handle dependencies.  This can come up, for instance, if you have complex scopes that require joins.  You might have some scopes that share the same needed set of joins to be valid, and you would like to spin that join out to a separate scope, so they could share it.  Or it might be called from a has_many :through, where that join will already be valid, but alternatively called directly, where the qualified column name suddenly won&amp;#8217;t make sense (&amp;#8216;other_table.column_name&amp;#8217; will be incorrrect in the unjoined query).&lt;/p&gt;


	&lt;p&gt;The best solution I have found is to use a class method that returns an &lt;a href="http://railscasts.com/episodes/112"&gt;anonymous scope.&lt;/a&gt;  This is a contrived example that could be better handled through the has_many side of the relationship, but with sufficiently complex multi-level has_many :throughs you can easily come up with a situation where you want dependent named_scopes.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class Book
  named_scope :author_join, :joins =&amp;gt; 'INNER JOIN authors ON books.author_id = authors.id, order =&amp;gt; 'authors.last_name, authors.first_name'
  named_scope :published, :conditions =&amp;gt; {:published =&amp;gt; true}

  def by_author_country(country_code)
    Book.author_join.scoped(:conditions =&amp;gt; ['authors.country_code = ?', country_code])
  end
end

#yes they are still composable
Book.by_author_country('EU').published
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Just beware that currently &lt;a href="http://blog.teksol.info/2008/5/26/why-are-activerecord-scopes-not-merged"&gt;scopes don&amp;#8217;t merge joins&lt;/a&gt;, so you can only have one joins per set of composed scopes.&lt;/p&gt;</description>
      <pubDate>Mon, 28 Jul 2008 12:42:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:7df76bc7-494a-420e-a1df-2c30c96d9ffe</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/07/28/named_scope-dependencies-via-returning-anonymous-scopes-from-class-methods</link>
      <category>Rails</category>
      <category>named_scopes</category>
    </item>
    <item>
      <title>Rails 2.1 and &amp;quot;RangeError: memory address is a recycled object&amp;quot; errors when running tests</title>
      <description>If you are hitting mysterious warnings of the format: &amp;#8220;RangeError: 0&amp;#215;19ad692 is recycled object&amp;#8221; in your tests, after upgrading to Rails 2.1, it might be due to threading issues.  Just commenting out the line
&lt;pre&gt;&lt;code&gt;
require 'thread'
&lt;/code&gt;&lt;/pre&gt;
in a library at work, that will run without it, quieted the tests, so it&amp;#8217;s possible even just requiring it will cause problems in Rails 2.1 (using ruby 1.8.6 p114).

	&lt;p&gt;Which is odd, since thread is required at least one place in the AR code itself, and elsewhere in our app.  Maybe it&amp;#8217;s an interplay of a couple libraries together, such as the non-threadsafe &amp;#8216;aws/s3&amp;#8217; and thread?&lt;/p&gt;</description>
      <pubDate>Thu, 24 Jul 2008 15:00:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:81ca1e55-266e-4855-be3c-f70cabec919a</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/07/24/rails-2-1-and-rangeerror-memory-address-is-a-recycled-object-errors-when-running-tests</link>
      <category>Rails</category>
      <category>rails</category>
      <category>errors</category>
      <category>upgrading</category>
    </item>
    <item>
      <title>Block form of gsub - passing backreferences to parsing functions</title>
      <description>&lt;pre&gt;&lt;code&gt;
def hello(message)
  "#{messsage}!" 
end
"1234foo4567foo".gsub /(\d*)foo/, hello($1)
=&amp;gt; "!!" 
"1234foo4567foo".gsub /(\d*)foo/, hello($1)
=&amp;gt; "4567!4567!" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I needed to pass a backreference ($1 or \1) from a regexp into a parsing method and then replace the match with the result.  I tried many ways and couldn&amp;#8217;t quite figure out why I couldn&amp;#8217;t do it, before realizing that the $n variables get populated based on the last match, so won&amp;#8217;t be populated when passed into the method call in the substitution param, or will be populated from the last time it was run.  And the &amp;#8217;\1&amp;#8217; for won&amp;#8217;t work for similar, if slightly different reasons.&lt;/p&gt;


	&lt;p&gt;Then I discovered the block form of gsub, which passes in each match to the block, and populates all the relevant regexp backreference/match globals:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
"1234foo4567foo".gsub(/(\d*)foo/){|m| hello($1)}
=&amp;gt; "1234!4567!" 
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Tue, 15 Jul 2008 14:24:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:6ecfb458-23d8-463f-a2d6-f8f4c6ea1b03</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/07/15/block-for-of-gsub-passing-backreferences-to-parsing-functions</link>
      <category>Rails</category>
      <category>regexp</category>
      <category>blocks</category>
    </item>
    <item>
      <title>Rails 2 foxy fixtures and named_scope/has_finder closure issues</title>
      <description>If you are using a has_finder (or possibly named_scope, I haven&amp;#8217;t confirmed this myself), remember that unless you wrap your condition =&amp;gt; in a lambda it is going to be evaluated early &amp;#8211; or at least earlier than fixture creation, it seems.  At work we had a case where one model had a finder that depended on its belong_to being in a subset of another, basically:
&lt;pre&gt;&lt;code&gt;
has_finder :all_active, {  :conditions =&amp;gt; {:status_id =&amp;gt; Status.active_ids} }
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This work in production, where the ids don&amp;#8217;t change, but since we are using the &lt;a href="http://ryandaigle.com/articles/2007/10/26/what-s-new-in-edge-rails-fixtures-just-got-a-whole-lot-easier"&gt;newer fixture approach&lt;/a&gt; on this model the ids are created dynamically.  Given how fixtures load, the table will probably be populated with the old values, at parse time for that class, then the test will run, and the fixtures will reload, and they will not match anymore.&lt;/p&gt;


This is easily fixed, when you realize what is happening:
&lt;pre&gt;&lt;code&gt;
has_finder :all_active, lambda {|| {  :conditions =&amp;gt; {:status_id =&amp;gt; OtherModel.active_ids} }}
&lt;/code&gt;&lt;/pre&gt;</description>
      <pubDate>Fri, 11 Jul 2008 16:20:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:a363a7c5-ca8e-48c1-89b2-77c5449a1220</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/07/11/rails-2-foxy-fixtures-and-named_scope-has_finder-closure-issues</link>
      <category>Rails</category>
      <category>Testing</category>
      <category>rails</category>
      <category>fixtures</category>
      <category>has_finder</category>
    </item>
    <item>
      <title>Designer looking for work, preferably in San Francisco</title>
      <description>&lt;p&gt;A friend and former employee of mine, &lt;a href="http://www.gridmuseum.com/"&gt;Stephanie Geerlings&lt;/a&gt; is looking for work.  She just put her new portfolio site up, so it&amp;#8217;s missing a couple things, like a contact link, so go ahead and email her at stillsmall at gmail if you are interested. If it doesn&amp;#8217;t go without saying, she comes recommended by me. ;)&lt;/p&gt;</description>
      <pubDate>Thu, 03 Jul 2008 10:08:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:febaebf1-45ca-449c-b44a-dd9d08f42a63</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/07/03/designer-looking-for-work-preferably-in-san-francisco</link>
      <category>Misc</category>
    </item>
    <item>
      <title>Year of the Infographic</title>
      <description>&lt;p&gt;This year was great for infographics of the primary season, over at the NYTimes.  Good data visualizations definitely give you a better grasp on the data, and you can see all at once things that might have been hidden before.  It seems to me creation of good infographics is a here to stay needed skill for online journalism and reporting now.&lt;/p&gt;


	&lt;p&gt;For instance, check this out to see that yes, despite the horse-race the media might make of it, &lt;a href="http://www.fivethirtyeight.com/"&gt;Obama is going to win the general.&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 02 Jul 2008 08:49:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:ef3d92e7-4708-4453-91ad-e2b877753ed5</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/07/02/year-of-the-infographic</link>
      <category>Misc</category>
      <category>infographics</category>
      <category>visualization</category>
      <category>data</category>
      <category>election</category>
    </item>
    <item>
      <title>Using AdSense in an FBML Facebook app</title>
      <description>&lt;p&gt;I wrote an article on the work blog on the basics of &lt;a href="http://blog.yieldbuild.com/2008/06/17/how-to-use-adsense-in-a-facebook-app/"&gt;how (and why it has t be done this way) to use Google AdSense in a FaceBook &lt;span class="caps"&gt;FBML&lt;/span&gt; app.&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 18 Jun 2008 10:09:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:87bfcf62-3294-432c-94bc-da7b729e4d09</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/06/18/using-adsense-in-an-fbml-facebook-app</link>
      <category>Misc</category>
      <category>facebook</category>
      <category>fbml</category>
    </item>
    <item>
      <title>Facebook Ajax on Public Canvas does not work unless loggged in</title>
      <description>It does not matter if you set 
&lt;pre&gt;&lt;code&gt;
ajax.requireLogin = false
&lt;/code&gt;&lt;/pre&gt;
it will still just error out.  And may I mention that the facebook ajax error gives NO diagnostic information.  There is an error handler you can set, but it accepts no parameters, it is just a function call, so you can say, &amp;#8220;Ooops, there was an error, who knows why.&amp;#8221; 

	&lt;p&gt;Oddly enough, it works fine in Safari, so I didn&amp;#8217;t notice it until I tried to debug something in Firebug.  Then I tried IE to compare, and realized that this was only working in Safari, of all things.  That&amp;#8217;s one step better than having an Opera only feature fer crissakes.  Anyways, here is the &amp;#8220;bug in the facebook bugzilla on ajax throwing an error unless the user is logged in&amp;#8221;:
http://bugs.developers.facebook.com/show_bug.cgi?id=1009&lt;/p&gt;</description>
      <pubDate>Tue, 03 Jun 2008 09:21:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:7b771f4b-bb73-4736-a78d-736053387fa0</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/06/03/facebook-ajax-on-public-canvas-does-not-work-unless-loggged-in</link>
      <category>Misc</category>
      <category>facebook</category>
    </item>
    <item>
      <title>Blog back up after some hiccups moving to Dreamhost PS</title>
      <description>&lt;p&gt;I&amp;#8217;ve spent plenty of time kvetching about problems running my Typo blog on Dreamhost.  I acknowledge, though, that despite some of DH&amp;#8217;s other hiccups, the core problem is that running Rails, on Apache fcgi, on a shared host is a Bad Idea.  And while I can handle the initial set-up of a slice/VPS I don&amp;#8217;t want to deal with the maintenance.  And I don&amp;#8217;t want to pay an arm and a leg for a fully managed set-up, just to host small sites like blog.&lt;/p&gt;


	&lt;p&gt;So when my site started going down constantly, again, due to processing getting killed for being over the memory limit, most likely, I decided I would try out Dreamhost&amp;#8217;s new &amp;#8220;Private Server&amp;#8221; offering, which is sort of a managed pseudo-VPS: cheaper than a true &lt;span class="caps"&gt;VPS&lt;/span&gt;, no root, they manage everything, but you do get virtualized/dedicated resources.  Sounds perfect for what I need.&lt;/p&gt;


	&lt;p&gt;Unfortunately, there is a waiting list&amp;#8230; and my site is down now!  So I decide to email support and say, hey, I&amp;#8217;ve sent some business their way over the years, my site is down now, any way to bump me to the front of the list.  Well they did &amp;#8211; but apparently side-stepping their usual processes.  I show as still wait-listed in their web, panel, but all the sites go down on my account, all of a sudden, from the switch. It turns out they have moved me over to the PS, but the &lt;span class="caps"&gt;DNS&lt;/span&gt; didn&amp;#8217;t get updated correspondingly for all my domains.&lt;/p&gt;


	&lt;p&gt;After a day or so of wrangling with their support queue, Jason got things fixed and it seems to be working.  Now I just need to see how well it works for my needs.  I&amp;#8217;m going to just stick with the Apache fcgi, for now, because it works, I&amp;#8217;m curious how the fcgi performs when not constantly being killed for exceeding the shared allowance of memory, and Dreamhost&amp;#8217;s proxy set-up proxies you to a single mongrel by default, so I&amp;#8217;d need to set-up up something like &lt;a href="http://blog.12spokes.com/articles/2008/02/18/using-nginx-as-a-load-balancing-proxy-to-a-mongrel-cluster-on-dreamhost"&gt;nginx to proxy to my mongrel clusters&lt;/a&gt;, and then keep all those up myself.  Not that big a deal, but I am going to run with the out of the box experience a bit first, and see how little admin work I can do.&lt;/p&gt;</description>
      <pubDate>Wed, 16 Apr 2008 23:19:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:745ad013-403b-4651-92d7-6449298c7906</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/04/16/blog-back-up-after-some-hiccups-moving-to-dreamhost-ps</link>
      <category>Rails</category>
      <category>Hosting</category>
      <category>dreamhost</category>
    </item>
    <item>
      <title>Getting circle/diamond/etc datapoints on an ActionScript dynamically created Flex charting lineseries</title>
      <description>&lt;code&gt;&lt;pre&gt;
myLineseries.setStyle("itemRenderer", new ClassFactory(DiamondItemRenderer));
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;It works similarly for lineSegmentRenderers, even custom ones, like &lt;a href="http://www.quietlyscheming.com/blog/2006/05/30/32/"&gt;Quietly Scheming&amp;#8217;s Dashed Line Renderer&lt;/a&gt;.  I just need to figure out how to set a parameter for the renderer created by the class factory now, so I can use different patterns of spaces to make different dashed and dotted lines.&lt;/p&gt;</description>
      <pubDate>Fri, 11 Apr 2008 11:40:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:1ea78ee7-dab2-47f0-b0fa-6dbaad7c7cb7</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/04/11/getting-circle-diamond-etc-datapoints-on-an-actionscript-dynamically-created-flex-charting-lineseries</link>
      <category>Misc</category>
      <category>flex</category>
      <category>charting</category>
      <category>actionscript</category>
    </item>
  </channel>
</rss>
