Andrew Arrow
Andrew Arrow

Reputation: 4575

does a ruby on rails rack class get access to the entire rails environment?

that is when the

def call(env)

method is invoked by hitting any url, can I inside that method make some ActiveRecord queries, use classes defined in lib, etc. etc.

Or is it more like an irb console without the rails env loaded? Another way to put it with a rake task example:

 task :foo => :environment do
    # with env
  end

 task :foo2 do
    # without env
  end

I would think rack classes would NOT get the environment so they are super fast and don't take all the overhead of a normal rails request.

But that doesn't seem to be the case. I CAN make ActiveRecord queries inside my rack class. So what is the advantage of rack then?

Upvotes: 0

Views: 113

Answers (1)

rogerdpack
rogerdpack

Reputation: 66751

It appears from http://www.ruby-on-rails-outsourcing.com/articles/2010/05/28/how-to-create-your-own-rack-middleware-class-in-ruby-on-rails/ that "env" is part of what rack "uses" for its requests, so you can't get around that (but I guess you can use it to avoid running the full rails stack, if you so desire).

There was once a "metal" option in rails 2 that basically avoided all loading. Apparently it was removed in rails 3. http://tektastic.com/2010/07/rails3-rack-and-where-did-my-metal-go.html you have to use a rack middleware instead. I'm unsure if this causes much performance difference (having to use rack) or not.

Upvotes: 1

Related Questions