More virtual hosts on one rails application

How can I access one rails application through more virtual hosts. The
different virtual hosts would differ in the database, images,
stylesheets and in some cases the layout of the views. What do you
recommend for realisation of serving multiple applications by one
rails code, which is accessed through different virtual hosts?

Ideally I’d like to:
* I prefer rails to distinguish which db/styles/images/layouts
have to use based on the host name.
* There may be different rails applications running, but above the
same code.
* Db connections might be established during the application

I am wondering which layer the rails app should recognise the host?
Should it be on the middleware, in boot scripts or elsewhere?

Thanks for your help,