<?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>ninajansen.dk &#187; Ruby on Rails</title>
	<atom:link href="http://ninajansen.dk/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://ninajansen.dk</link>
	<description></description>
	<lastBuildDate>Thu, 23 Apr 2009 21:46:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fragment cache expiry from background jobs is a hot mess</title>
		<link>http://ninajansen.dk/2008/06/11/fragment-cache-expiry-from-background-jobs-is-a-hot-mess/</link>
		<comments>http://ninajansen.dk/2008/06/11/fragment-cache-expiry-from-background-jobs-is-a-hot-mess/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 07:55:48 +0000</pubDate>
		<dc:creator>Nina Jansen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[rant]]></category>
		<category><![CDATA[sweepers]]></category>

		<guid isPermaLink="false">http://ninajansen.dk/?p=39</guid>
		<description><![CDATA[In order to get a cache sweeper to actually do anything when it is triggered, it must be called from a controller, and that controller must be instantiated though a http request.
This is what I have learned from trying to expire fragment cache from background scripts in Ruby om Rails. It simply is not possible. [...]]]></description>
			<content:encoded><![CDATA[<p>In order to get a cache sweeper to actually do anything when it is triggered, it must be called from a controller, and that controller must be instantiated though a http request.</p>
<p>This is what I have learned from trying to expire fragment cache from background scripts in Ruby om Rails. It simply is not possible. If a controller is not instantiated when you hit the expire_fragment method<br />
(or any other cache sweeper method) you get a nil reply.</p>
<p>I understand why this decision has been made: Fragments are in views. Only the controller should deal with views. The controller is instantiated when it is requested by the web-server. Otherwise you break the MVC paradigm. Except that this mental model does not take into account the fact that<br />
every web-application (except the simplest ones) have stuff going on in the background. Long calculations are made by script so the user doesn&#8217;t have to wait while they are being finished. Stuff is done at a given time of day, like expiring old subscriptions. This is why we have BackgroundDRB and BJ. Background jobs need access to the fragment cache. If anyone who is really into rails could find a way to let background jobs do fragment cache things, I&#8217;d be very grateful.</p>
<p>For now, I have to periodically expire or reset all fragments because I know that background jobs periodically change the data used to generate them. Most inelegant and inefficient.</p>
<img src="http://ninajansen.dk/?ak_action=api_record_view&id=39&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://ninajansen.dk/2008/06/11/fragment-cache-expiry-from-background-jobs-is-a-hot-mess/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing caching with memcache, cache_fu and rspec</title>
		<link>http://ninajansen.dk/2008/05/21/testing-caching-with-memcache-cache_fu-and-spec/</link>
		<comments>http://ninajansen.dk/2008/05/21/testing-caching-with-memcache-cache_fu-and-spec/#comments</comments>
		<pubDate>Wed, 21 May 2008 14:06:24 +0000</pubDate>
		<dc:creator>Nina Jansen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://ninajansen.dk/?p=37</guid>
		<description><![CDATA[I use cache_fu for caching my rails application. I use a &#8220;fat models, skinny controllers strategy&#8221;. This means that my Author model has code like this:

def cached_books
get_cache(:author_books) do
Book.find_by_author_id(self.id)
end
end

This method caches all books belonging to an author. Cache fu does not cache belongs_to or has_(one&#124;many) relationships, and that&#8217;s why we have this method. But this also [...]]]></description>
			<content:encoded><![CDATA[<p>I use cache_fu for caching my rails application. I use a &#8220;fat models, skinny controllers strategy&#8221;. This means that my Author model has code like this:</p>
<p><code><br />
def cached_books<br />
get_cache(:author_books) do<br />
Book.find_by_author_id(self.id)<br />
end<br />
end<br />
</code></p>
<p>This method caches all books belonging to an author. Cache fu does not cache belongs_to or has_(one|many) relationships, and that&#8217;s why we have this method. But this also means that if we add a book to an author, we must also expire the author records cache. This can all be done with cache_fu. The problem is that these relationships might get quite complicated and it would be nice to be able to write some specs that test that everything behaves as intended.</p>
<p>Chris Wanstrath, the author of cache_fu, suggests using mocha to stub everything about, but didn&#8217;t want to emulate the caching behavior and then test if the behavior is right. That is fine if you test the cache_fu plugin, but not fine if you need to test if your application uses the cache_fu plugin correctly. What I wanted was to turn on caching while running my specs and then query the cache to see if things are actually cached properly.</p>
<p>This first this I tried was turning on caching in the &#8220;test&#8221; environment. Turns out that it is not a good idea, as a lot of specs go into some kind of recursive error when you do that. Caching should be off by default in test. I needed a way to turn on the cache functions in a given spec.</p>
<p>Long story short: for unit testing you add the following function to your model:</p>
<p><code><br />
class &lt;&lt; self<br />
def reenable_cache_for_test<br />
class &lt;&lt; self<br />
alias_method :fetch_cache, :fetch_cache_without_disabled<br />
alias_method :set_cache, :set_cache_without_disabled<br />
alias_method :expire_cache, :expire_cache_without_disabled<br />
end<br />
end<br />
end<br />
</code></p>
<p>You call this method in your spec like this:</p>
<p><code><br />
it "should cache author" do<br />
Author.reenable_cache_for_test</code><br />
<code><br />
CACHE.flush_all<br />
CACHE.get('Author:1').should == nil<br />
author = Author.get_cache(1)<br />
author.should != nil<br />
CACHE.get('Author:1').should == author<br />
</code><code><br />
CACHE.get('Author:1:books').should == nil<br />
books = user.cached_books<br />
CACHE.get('Author:1:books).should == books<br />
end<br />
</code></p>
<p>Fragment caching is a bit different, in you controller spec you do this:</p>
<p><code><br />
it "fragment should be cached on get" do<br />
ActionController::Base.perform_caching = true<br />
ActsAsCached.config.clear<br />
config = YAML.load_file( File.dirname(__FILE__) + '/../../vendor/plugins/cache_fu/defaults/memcached.yml.default')<br />
config['test'] = config['development'].merge('benchmarking' =&gt; false, 'disabled' =&gt; false)<br />
ActsAsCached.config = config<br />
ActsAsCached::FragmentCache.setup!</code><br />
<code><br />
CACHE.flush_all<br />
</code><br />
&#8230; and then do your cache checking.</p>
<img src="http://ninajansen.dk/?ak_action=api_record_view&id=37&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://ninajansen.dk/2008/05/21/testing-caching-with-memcache-cache_fu-and-spec/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cache sweepers in Rails, put them in app/models!</title>
		<link>http://ninajansen.dk/2008/05/14/cache-sweepers-in-rails-put-them-in-appmodels/</link>
		<comments>http://ninajansen.dk/2008/05/14/cache-sweepers-in-rails-put-them-in-appmodels/#comments</comments>
		<pubDate>Wed, 14 May 2008 15:41:50 +0000</pubDate>
		<dc:creator>Nina Jansen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[sweepers]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ninajansen.dk/?p=33</guid>
		<description><![CDATA[So i&#8217;ve been spending hours tracking this one down: I wanted to have cache sweepers in rails and followed the advice from railscasts and railsevny. Those tutorials tell you to put sweepers in an app/sweepers directory and add that directory to your load path. DON&#8217;T DO THAT. That simply did not work for me (using [...]]]></description>
			<content:encoded><![CDATA[<p>So i&#8217;ve been spending hours tracking this one down: I wanted to have cache sweepers in rails and followed the advice from <a href="http://railscasts.com/episodes/89">railscasts</a> and <a href="http://www.railsenvy.com/2007/2/28/rails-caching-tutorial">railsevny</a>. Those tutorials tell you to put sweepers in an app/sweepers directory and add that directory to your load path. DON&#8217;T DO THAT. That simply did not work for me (using rails 2.02). After banging my head against google for hours, I found out that sweeper simply has to go in app/models, MVC be dammed. If they are put there they get loaded, and you can add them as active record observers. If they are not put there rails won&#8217;t find them. I don&#8217;t know if this is a bug or a feature, but that is just the way it is.</p>
<img src="http://ninajansen.dk/?ak_action=api_record_view&id=33&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://ninajansen.dk/2008/05/14/cache-sweepers-in-rails-put-them-in-appmodels/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>In place editing in rails</title>
		<link>http://ninajansen.dk/2008/05/13/in-place-editing-in-rails/</link>
		<comments>http://ninajansen.dk/2008/05/13/in-place-editing-in-rails/#comments</comments>
		<pubDate>Tue, 13 May 2008 17:06:01 +0000</pubDate>
		<dc:creator>Nina Jansen</dc:creator>
				<category><![CDATA[REST]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ninajansen.dk/?p=32</guid>
		<description><![CDATA[So basically, the official plugin is old and cant really deal with restful routing. For some reason, when I used the url_for method in my views, the in place editor javascript function couldn&#8217;t interpret the (restful) route properly and sent a post request to &#8216;/controller/1/edit&#8217; to an action named &#8220;1&#8243; with an id set to [...]]]></description>
			<content:encoded><![CDATA[<p>So basically, the official plugin is old and cant really deal with restful routing. For some reason, when I used the url_for method in my views, the in place editor javascript function couldn&#8217;t interpret the (restful) route properly and sent a post request to &#8216;/controller/1/edit&#8217; to an action named &#8220;1&#8243; with an id set to &#8220;edit&#8221;, which obviously didn&#8217;t do anything useful. So instead I called the in place editor script in the following manner (I&#8217;m using haml btw.):</p>
<p><code><br />
%span{:id =&gt; "name", :class =&gt; "in_place_editor_field"}= @user.name<br />
= in_place_editor "name", {:url =&gt; '/user/edit/' + @user.id.to_s, :rows =&gt; 1, :size =&gt; 32 }<br />
</code></p>
<p>So basically I hard coded the route so it would fall back on standard routing. Yes I know that:</p>
<ul>
<li> This is ugly as sin</li>
<li> I&#8217;m supposed to do a PUT request to a function named update, not a post request to a function named edit.</li>
</ul>
<p>But it solved the problem.</p>
<img src="http://ninajansen.dk/?ak_action=api_record_view&id=32&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://ninajansen.dk/2008/05/13/in-place-editing-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails tips #1</title>
		<link>http://ninajansen.dk/2008/05/02/rails-tips-1/</link>
		<comments>http://ninajansen.dk/2008/05/02/rails-tips-1/#comments</comments>
		<pubDate>Fri, 02 May 2008 11:02:42 +0000</pubDate>
		<dc:creator>Nina Jansen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://ninajansen.dk/?p=25</guid>
		<description><![CDATA[These might be completely obvious to everyone, but I found them useful:

 If you want to migrate a specific database, you can do it by setting RAILS_ENV on the command line. Like this:
rake db:migrate RAILS_ENV=production
 If you want to see all the rake tasks in your application with a description of what they do, execute:
rake [...]]]></description>
			<content:encoded><![CDATA[<p>These might be completely obvious to everyone, but I found them useful:</p>
<ul>
<li> If you want to migrate a specific database, you can do it by setting RAILS_ENV on the command line. Like this:<br />
<code>rake db:migrate RAILS_ENV=production</code></li>
<li> If you want to see all the rake tasks in your application with a description of what they do, execute:<br />
<code>rake -D</code></li>
</ul>
<img src="http://ninajansen.dk/?ak_action=api_record_view&id=25&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://ninajansen.dk/2008/05/02/rails-tips-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
