How to access session, cookies, params, request in Model !! 20

Posted by pratik
on Thursday, October 18

For fuck’s sake, STOP asking this question in IRC

You’re completely/totally/fucking wrong if you want to access sessions, params, cookies, etc. in your Models.

You can’t get more wrong than this in rails world probably. But some people just don’t get it. So if you must, this is how you can do it ( I’d suggest you go back to PHP or whatever ) :

Add this code at the bottom of your application.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class ApplicationController < ActionController::Base
  # Your existing stuff
  around_filter :you_dont_have_bloody_clue
  
  protected
  
  def you_dont_have_bloody_clue
    klasses = [ActiveRecord::Base, ActiveRecord::Base.class]
    methods = ["session", "cookies", "params", "request"]
    
    methods.each do |shenanigan|
      oops = instance_variable_get(:"@_#{shenanigan}") 
      
      klasses.each do |klass|
        klass.send(:define_method, shenanigan, proc { oops })
      end
    end
    
    yield
    
    methods.each do |shenanigan|      
      klasses.each do |klass|
        klass.send :remove_method, shenanigan
      end
    end
    
  end
end

Again, do it if you wish, just don’t ask anyone how to do it ever again!