views:

60

answers:

1

I have an app that uses subdomains for each 'account'.

From what I have read it is good practice to "Tie all top-level requests off the current account (subdomain)".

e.g.

def find_users
  @users = @current_account.users
end

Simple enough. But when I start having deeply nested routes, I can't use shallow routing without losing the scope of the subdomain.

So how do I achieve shallow routing and keep this integrity?

The only things I have thought of are:

  1. To include a foreign key to the top level (subdomain) in some of the more deeply nested models. But this seems a bit of a hack.

  2. To use a before_filter to backtrack up the associations and check that the subdomain holds true. This seems more logical but still feels not great.

Does anyone have a take on this?

A: 

I've been playing with acts_as_restricted_subdomain lately where a foreign key is used, and found it incredibly easy to use:

http://github.com/penguincoder/acts_as_restricted_subdomain

It overrides the default behaviour of ActiveRecord so if you have an account/subdomain specified User.find(123) becomes the same as Subdomain.users.find(123) which makes writing code alot cleaner because it allows you write your application without worrying about the constraints of subdomains.

The main disadvantage to having the foreign key is the possibility of db queries taking longer to perform as your dataset scales, but putting in appropriate named indices should mitigate this.

Mark Connell
Thanks I will check the link out
Cameron