I think that a better way would be to create a separate database per
organization. Every organization could have their own subdomain. And
you could change the database connection during each request to the
proper database. Another way would be to extend every table primary
key with :organization_id. Of course to do that you would have to use
plugin that adds composite primary keys support to active record.

Depending on the application code, if it is not too complicated and
you always you AR queries (I mean, you do not genery by yourself sql
queries) you could make a solution like this:

#application controller

before_filter :find_organization
around_filter :within_organization

private

def find_organization
  @organization = Organization.find(session[:organization_id])
end

def within_organization
  ActiveRecord::Base.send(:with_scope, :find => where("organization_id
= ?", @organization.id")) do
    yield
  end
end

I am not sure whether it could works. Maybe you would have to do it
with every class instead...


def within_organization
  User.send(:with_scope, :find => where("organization_id = ?",
@organization.id")) do
    Article.send(:with_scope, :find => where("organization_id = ?",
@organization.id")) do
      Comment.send(:with_scope, :find => where("organization_id = ?",
@organization.id")) do
        yield
      end
    end
  end
end

Robert Pankowecki

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to