views:

30

answers:

2

How do I tell if rails is running under a web server, as opposed to a script or irb?

For context: I'm tracking an ActiveRecord object, and I want to email alerts if the objects changes, but only if the changes came through the web interface.

+1  A: 

Perhaps I'm missing something obvious but isn't the fact that if your model access occurs through a controller interface (i.e. some action) then, by definition, an access through a web interface?

Rails doesn't run under irb. Yes, you can access objects and state under the console but that's not a web server access.

I think you just need to set up some filters in your controllers to implement your tracking logic for the models of interest and then you can differentiate between general access and web-server access (which is controller initiated).

Make sense?

bjg
Hey bjg, thanks for your response. When I say irb, I mean console. Filters are a great idea, but I'm not exactly clear how that would work. The controller in question isn't very RESTful, and updates several different models. How would I detect this from a filter?
rogueg
A: 

I've found something that works, though it's not very pretty

$0  # => "irb" (for script/console) 
    # => "/some/handler.fcgi" (for webserver)
    # => "/some/script.rb" (for a script)
rogueg