views:

76

answers:

2

I feel like I'm just missing an environmental variable here, but is there anyway to get the FQDN and port that a Rails server is listening on? I feel like there should be a variable like RAILS_URL or something. I'd like to access it from a model.

A: 

I've gotten around this by setting an environment variable with the application's URL as part of the script that runs Mongrel. The env. var. is then available to Ruby, and you can even set a global like RAILS_ROOT in /config/environment.rb

You can also investigate the request object: http://perma-link.appspot.com/k

request.domain
request.port

Pass values returned from the request object into the Model when needed.

class Bar < AR::Base 
  def self.active_for_domain(domain)
    find(:all, :conditions => ["deleted <> true and domain = ?", domain])
  end
end

class FooController < ApplicationController
  def index
    @bars = Bar.active_for_domain(request.domain)
  end
end
Terry Lorber
I am actually doing the environment.rb way now, but I'd much rather have it set programatically.The problem with using request is that I want to access the information in a Model, while the request is only accessible through a controller.
Fotios
A: 

I think you are looking for root_url.

scrrr