I use Apartment work in my Rails 4 application.
I followed this step :
1)
#config/initializer/apartement.rb
require 'apartment/elevators/generic'
2)
#config/initializer/application.rb
config.middleware.use 'Apartment::Elevators::Generic', Proc.new {|request|
remember_token = request.cookies['remember_token']
if remember_token.blank?
nil
else
User.find_by(remember_token: remember_token).backoffice_company_id
end
}
This works fine but it's not efficient because a request is done on the database on each request on the application.
How can I avoid this database access when the remember_token is unchanged from the last request (because it's the same user) ?
I tried to store this in a session variable but it's seems to be global à this level (application.rb) when 2 users use the application.
Thanks
Bruno
Came across this setting something similar up today for specific subdomain routing on main domain, but domain-level routing on others. Digging into the code that ships with apartment, they use tenant.presence
, rather than ending with the return of the if/else. Checking presence ( http://api.rubyonrails.org/classes/Object.html#method-i-presence ) essentially gives an ||=
inline while checking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.