1

Background

I have a multi-tenant Rails application which uses the Apartment gem to switch schema depending on their subdomain when they login. The User and Account entities are in the public schema. All of the other related entities are in the account specific schema.

Question

When a new user signs up, the User and Account records are created during the initial form post (i.e., in RegistrationsController#create).

However, I would like to create other records at this time (e.g, a Family record), but the account specific schema hasn't been created yet and the context hasn't switched, so if I do something like this:

@resource.create_family(name: @resource.last_name)

The Family record will be incorrectly created in the public schema.

Devise lets you specify a after_sign_up_path_for value, so I was thinking of maybe creating a custom action in the FamiliesController that does the work of creating new record, but that seems kind of kludgy and non RESTful:

def after_sign_up_path_for(resource)
  # root_url(:subdomain => resource.account.subdomain)
  family_autocreate_url(:subdomain => resource.account.subdomain, :query => resource.last_name)
end

What's the right way to do this?

(Note: the schema context isn't switched until I redirect to a URL with the account specific subdomain. Hence the thought of doing it )

Rob Sobers
  • 19,173
  • 22
  • 78
  • 107
  • Apartment allows you to manually switch context with Apartment::Tenant.switch!('tenant_name'). What is the problem if in RegistrationsController, after an account is created, you manually switch context and create the associated family object? – Hoa Nov 29 '14 at 17:29

0 Answers0