<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Making Session Data Available to Models in Ruby on Rails</title>
	<atom:link href="http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/</link>
	<description>Musings of a software developer in Milwaukee, WI.</description>
	<pubDate>Thu, 20 Nov 2008 17:07:28 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Daniel</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-14226</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Wed, 01 Oct 2008 13:26:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-14226</guid>
		<description>Instead of cache_sweeper i use a similar implementation. Just include Auditing to your controller and your observer should inherit the Auditing::Observer. This is a lot cleaner approach than using TLS which might be server/implementation specific.

&lt;code lang="ruby"&gt;
 module Auditing
  def self.included(base)
    ActiveRecord::Base.observers.each do &#124;observer&#124;
      observer = if observer.respond_to?(:to_sym)
        observer.to_s.camelize.constantize.instance
      elsif observer.respond_to?(:instance)
        observer.instance
      else
        raise ArgumentError, "#{observer} is an invalid class name"
      end
      base.around_filter(observer) if observer.is_a?(Auditing::Observer)
    end
  end

  class Observer &#60; ActiveRecord::Observer
    attr_accessor :controller

    def before(controller)
      self.controller = controller
    end

    def after(controller)
      self.controller = nil
    end
  end

end
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Instead of cache_sweeper i use a similar implementation. Just include Auditing to your controller and your observer should inherit the Auditing::Observer. This is a lot cleaner approach than using TLS which might be server/implementation specific.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;"> <span style="color:#9966CC; font-weight:bold;">module</span> Auditing
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">observers</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |observer|
      observer = <span style="color:#9966CC; font-weight:bold;">if</span> observer.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:to_sym</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        observer.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">camelize</span>.<span style="color:#9900CC;">constantize</span>.<span style="color:#9900CC;">instance</span>
      <span style="color:#9966CC; font-weight:bold;">elsif</span> observer.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:instance</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        observer.<span style="color:#9900CC;">instance</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#996600;">&quot;#{observer} is an invalid class name&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      base.<span style="color:#9900CC;">around_filter</span><span style="color:#006600; font-weight:bold;">&#40;</span>observer<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> observer.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Auditing::Observer</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> Observer <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Observer</span>
    attr_accessor <span style="color:#ff3333; font-weight:bold;">:controller</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> before<span style="color:#006600; font-weight:bold;">&#40;</span>controller<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">controller</span> = controller
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> after<span style="color:#006600; font-weight:bold;">&#40;</span>controller<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">controller</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-11081</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Thu, 29 Nov 2007 19:04:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-11081</guid>
		<description>Is there a simple way to explicitly specify that your app requires mongrel version &#60;= (whatever version is current at the time of writing)?  Cause then this is actually not so bad.</description>
		<content:encoded><![CDATA[<p>Is there a simple way to explicitly specify that your app requires mongrel version &lt;= (whatever version is current at the time of writing)?  Cause then this is actually not so bad.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prateek</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-11007</link>
		<dc:creator>Prateek</dc:creator>
		<pubDate>Thu, 25 Oct 2007 07:38:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-11007</guid>
		<description>I am reading rails recipes and they mention "Cache Sweepers" as a possible solution for this problem. I am yet to try it out, but you can look at that as an alternative too

Regards
Prateek</description>
		<content:encoded><![CDATA[<p>I am reading rails recipes and they mention &#8220;Cache Sweepers&#8221; as a possible solution for this problem. I am yet to try it out, but you can look at that as an alternative too</p>
<p>Regards<br />
Prateek</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SynTruth</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10940</link>
		<dc:creator>SynTruth</dc:creator>
		<pubDate>Wed, 05 Sep 2007 14:07:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10940</guid>
		<description>I got around this in another way.  I created a lib in the {RAILS_ROOT}/lib directory that was loaded in config/enviroment.rb.  It contains helper functions like is_logged_in?() and get_user_id() that checked for @session and @session[:user] and such, and returned things as needed, these are callable from within the model structure since they are global.</description>
		<content:encoded><![CDATA[<p>I got around this in another way.  I created a lib in the {RAILS_ROOT}/lib directory that was loaded in config/enviroment.rb.  It contains helper functions like is_logged_in?() and get_user_id() that checked for @session and @session[:user] and such, and returned things as needed, these are callable from within the model structure since they are global.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niko</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10892</link>
		<dc:creator>Niko</dc:creator>
		<pubDate>Thu, 16 Aug 2007 12:00:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10892</guid>
		<description>I've no idea wether this concern is valid, but will it work with the Evented Mongrel Swiftiply is using? Is it using one thread per request, too?</description>
		<content:encoded><![CDATA[<p>I&#8217;ve no idea wether this concern is valid, but will it work with the Evented Mongrel Swiftiply is using? Is it using one thread per request, too?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kai Krakow</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10891</link>
		<dc:creator>Kai Krakow</dc:creator>
		<pubDate>Thu, 16 Aug 2007 10:14:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10891</guid>
		<description>Very nice article. It helped me tracking the session user in an audit log which would otherwise have been pretty useless. But how safe is this to use on Apache2 mpm-worker with mod-fcgid? As far as I can see, the before_filter stores the session user in a local thread var on each request in the beginning, so the data stored there should be correct throughout the whole request. Does this hold true on Apache2 mpm-worker with mod-fcgid?</description>
		<content:encoded><![CDATA[<p>Very nice article. It helped me tracking the session user in an audit log which would otherwise have been pretty useless. But how safe is this to use on Apache2 mpm-worker with mod-fcgid? As far as I can see, the before_filter stores the session user in a local thread var on each request in the beginning, so the data stored there should be correct throughout the whole request. Does this hold true on Apache2 mpm-worker with mod-fcgid?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geoff Lane</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10880</link>
		<dc:creator>Geoff Lane</dc:creator>
		<pubDate>Sat, 11 Aug 2007 03:45:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10880</guid>
		<description>Alex,
That's a very good point. Luckily if you are using Mongrel to deploy your Rails app it creates a new thread to handle each request. If anyone is concerned, then just stick with the ActionController::Caching::Sweeper method mentioned in the article.</description>
		<content:encoded><![CDATA[<p>Alex,<br />
That&#8217;s a very good point. Luckily if you are using Mongrel to deploy your Rails app it creates a new thread to handle each request. If anyone is concerned, then just stick with the ActionController::Caching::Sweeper method mentioned in the article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10877</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 09 Aug 2007 07:03:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/#comment-10877</guid>
		<description>I've done similar things in other architectures.  This is useful, but if the container reuses threads (as can happen in J2EE) you can have serious security and other issues.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve done similar things in other architectures.  This is useful, but if the container reuses threads (as can happen in J2EE) you can have serious security and other issues.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
