Comments (11)
Thank you guys,
I fixed it with the following code
module Apartment
module Elevators
class MySubdomain < Apartment::Elevators::Subdomain
def call(env)
request = Rack::Request.new(env)
database = @processor.call(request)
begin
Apartment::Database.switch database if database
rescue Apartment::DatabaseNotFound, Apartment::SchemaNotFound
return [404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
@app.call(env)
end
end
end
end
from apartment.
hey sorry for the late response... that's basically what we do in our app also.
from apartment.
Please give me advice.
What file should I add the above code to?
from apartment.
@kizashi1122 this can live wherever you want. maybe app/middleware
?? It doesn't matter. Just as long as your module / classname corresponds to Rails autoloading. You could literally do this:
# lib/my_elevator.rb
class MyElevator < Apartment::Elevators::Subdomain
# ...
end
from apartment.
Thank you. It works fine.
But one more step needed to accomplish it in my case.
- Put the following file to lib/my_elevator.rb as @bradrobertson mentioned
require 'apartment/elevators/subdomain' # <- this line is necessary
class MyElevator < Apartment::Elevators::Subdomain
# ...
end
- Put some code to config/initializers/apartment.rb
require 'my_elevator'
# ...
Rails.application.config.middleware.use 'MyElevator' # instead of 'Apartment::Elevators::Subdomain'
Does it really work without the 2nd step?
Thanks,
from apartment.
no you need both. I was just explaining how to write the middleware.
For future reference this is nothing to do with Apartment itself. It's really just a rails question that could be asked on StackOverflow or something.
from apartment.
Thanks again.
Sorry about that.
But doesn't work with only 1st step. I have to learn Rails more.
I hope this feature will be implemented soon.
from apartment.
http://stackoverflow.com/questions/27188411/apartment-ruby-gem-want-to-catch-an-exception/28233828#28233828 this might help you.
from apartment.
Great help!
from apartment.
// apartment.rb
require 'rescued_apartment_middleware'
...
...
...
Rails.application.config.middleware.insert_before ActiveRecord::Migration::CheckPending, Apartment::Elevators::RescuedApartmentMiddleware
// lib/rescued_apartment_middleware.rb
require 'apartment/elevators/subdomain'
require 'apartment/tenant'
module Apartment
module Elevators
class RescuedApartmentMiddleware < Apartment::Elevators::Subdomain
def call(env)
request = Rack::Request.new(env)
# @processor is the current middleware instance and the call method will invoke the parse_tenant_name method.
# if you need to change the subdomain database name you can change it by defining the parse_tenant_name method below
database = @processor.call(request)
begin
Apartment::Tenant.switch!(database) if database
rescue Apartment::TenantNotFound, ActiveRecord::NoDatabaseError
Rails.logger.error "Error: Apartment says tenant was not found for #{database.inspect}"
raise ActionController::RoutingError.new('Not Found')
#return [404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
@app.call(env)
end
end
end
end
// application_controller.rb
rescue_from Apartment::TenantNotFound do |exception|
ActionController::RoutingError.new('Not Found')
end
the above code resolved my issue in Rails 4.2.5
from apartment.
I don't know why rescue is not working, anybody knows?
I had to changed it to:
module Apartment
module Elevators
class RescuedApartmentMiddleware < Apartment::Elevators::Subdomain
def call(env)
request = Rack::Request.new(env)
database = @processor.call(request)
if database
result = Apartment::Tenant.switch!(database) rescue "notfound"
if result == "notfound"
Rails.logger.error "Error: Apartment says tenant was not found for #{database.inspect}"
raise ActionController::RoutingError.new('Not Found')
else
@app.call(env)
end
else
@app.call(env)
end
end
end
end
end
from apartment.
Related Issues (20)
- Reverting schema based multi tenancy to single schema foreign key based multi tenancy HOT 1
- Rails 5: ActiveRecord::ConnectionNotEstablished: No connection pool with id primary found HOT 1
- Switch from subdomains to generic elevator HOT 1
- how can I redirect to tenant on creation? No devise or warden
- Doesn't work with Rails 6 HOT 1
- warning: instance variable not initialized HOT 1
- write RSpec tests for APIs HOT 1
- ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR: operator class "gin_trgm_ops" does not exist for access method "gin" HOT 1
- Apartment 2.2 is not compatible with ActiveRecord 3
- raw SQL doesn't get interpreted by apartment to use schemas
- 2.2.1 not compatible with Rails 6.1 HOT 1
- excluded_models does not works with db:setup HOT 1
- Making a request to an ActiveStorage resource fails occasionally
- Direct uploads writing to random tenants HOT 1
- Fresh install on Rails 7.0.4 can't setup HOT 2
- Not able to configure New Relic for each apartment separately
- Not able find a solution for multi db connection HOT 2
- Find with Parameter 1 but SQL query taking it as NULL
- update it .... HOT 1
- Error when trying to generate apartment initializer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apartment.