Hello, I''m running Mongrel 0.3.13.3, cluster 0.2.0, and Rails 1.1.6 and I have a problem with a sweeper not having access to the controller instance. NoMethodError (undefined method `session'' for nil:NilClass): /app/models/audit_sweeper.rb:16:in `log'' /app/models/audit_sweeper.rb:9:in `after_update'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ active_record/observer.rb:123:in `send'' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ active_record/observer.rb:123:in `update'' /usr/lib/ruby/1.8/observer.rb:185:in `notify_observers'' /usr/lib/ruby/1.8/observer.rb:184:in `each'' /usr/lib/ruby/1.8/observer.rb:184:in `notify_observers'' Here''s my code (the problem is with ''controller'' being nil): def log(record, event, user = controller.session[:user]) a = AuditTrail.new(:record_id => record.id, :record_type => record.type.name, :event => event, :user_id => user) a.record_version = record.version if record.respond_to?("version") a.save! end The interesting thing is that it only happens on actions within one of the controllers that are calling the cache sweeper (cache_sweeper :audit_sweeper). Any help or pointers would be much appreciated. Erik
On 9/14/06, Erik Morton <eimorton at gmail.com> wrote:> Hello, > I''m running Mongrel 0.3.13.3, cluster 0.2.0, and Rails 1.1.6 and I > have a problem with a sweeper not having access to the controller > instance. > > NoMethodError (undefined method `session'' for nil:NilClass): > /app/models/audit_sweeper.rb:16:in `log'' > /app/models/audit_sweeper.rb:9:in `after_update'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ > active_record/observer.rb:123:in `send'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ > active_record/observer.rb:123:in `update'' > /usr/lib/ruby/1.8/observer.rb:185:in `notify_observers'' > /usr/lib/ruby/1.8/observer.rb:184:in `each'' > /usr/lib/ruby/1.8/observer.rb:184:in `notify_observers'' > > Here''s my code (the problem is with ''controller'' being nil): > def log(record, event, user = controller.session[:user]) > a = AuditTrail.new(:record_id => record.id, :record_type => > record.type.name, > :event => event, :user_id => user) > a.record_version = record.version if record.respond_to?("version") > a.save! > end > > The interesting thing is that it only happens on actions within one > of the controllers that are calling the cache sweeper > (cache_sweeper :audit_sweeper). > > Any help or pointers would be much appreciated. > > ErikThis is an issue with your rails app, not mongrel. You probably need to specify which actions the cache sweeper is supposed to run on: cache_sweeper :audit_sweeper, :only => [:create, :update, :destroy] That method will set up an around filter using the Cache Sweeper class that sets the controller instance var. This brings up some possible issues, because the Cache Sweeper is still an Observer. Once it''s instantiated, it''s going to fire the callbacks on any model it''s observing, even if it''s not being saved on a controller it was configured for. So, it''s best to put a ''return if controller.nil?'' at the top of the cache sweepers, or just be ready to solve these issues if they come up. -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com
I''m actually doing similar code, and it''s working for me with the same setup. I do have the issue with trying to figure out how to test that sweeper code though... On 9/14/06, Erik Morton <eimorton at gmail.com> wrote:> Hello, > I''m running Mongrel 0.3.13.3, cluster 0.2.0, and Rails 1.1.6 and I > have a problem with a sweeper not having access to the controller > instance. > > NoMethodError (undefined method `session'' for nil:NilClass): > /app/models/audit_sweeper.rb:16:in `log'' > /app/models/audit_sweeper.rb:9:in `after_update'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ > active_record/observer.rb:123:in `send'' > /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/ > active_record/observer.rb:123:in `update'' > /usr/lib/ruby/1.8/observer.rb:185:in `notify_observers'' > /usr/lib/ruby/1.8/observer.rb:184:in `each'' > /usr/lib/ruby/1.8/observer.rb:184:in `notify_observers'' > > Here''s my code (the problem is with ''controller'' being nil): > def log(record, event, user = controller.session[:user]) > a = AuditTrail.new(:record_id => record.id, :record_type => > record.type.name, > :event => event, :user_id => user) > a.record_version = record.version if record.respond_to?("version") > a.save! > end > > The interesting thing is that it only happens on actions within one > of the controllers that are calling the cache sweeper > (cache_sweeper :audit_sweeper). > > Any help or pointers would be much appreciated. > > Erik > _______________________________________________ > Mongrel-users mailing list > Mongrel-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/mongrel-users >