Rails app takes a long time to generate error page - ruby-on-rails

My Rails app generates error page very slowly (Rail 3.1/3.2, ruby 1.9.2/1.9.3). E.g. I have added my_bad_variable to some .haml template and
Rendered fees/index.html.haml within layouts/application (97752.1ms)
Completed 500 Internal Server Error in 99579ms
ActionView::Template::Error (undefined local variable or method `my_bad_variable' for #<#:0x00000003bbf0c8>):
After deleting this fake variable:
Completed 200 OK in 327ms (Views: 274.7ms | ActiveRecord: 9.8ms)
Any suggestions?

I had this issue when I upgraded to rails 3.2. I added this initializer to fix it:
module ActionDispatch
module Routing
class RouteSet
alias :inspect :to_s
end
end
end
I think it was related to ree. Are you using ree?

Related

Rails: slow partial rendering when cache_classes is false

I'm upgrading a Rails 3.2 application up to version 5. The first thing that I'm doing is to upgrade to version 4 and make all tests are still passing.
One thing that I noticed is that my application is now very slow in development mode. Some requests are taking 1500ms when they used to take less than 50ms in 3.2.
After some debugging I found that if I enable cache_classes configuration in version 4, I will get a similar performance to the one that I used to have in version 3.2 with cache_classes disabled.
Rails 4.2.9 ( config.cache_classes = false )
(previous render messages intentionally removed )
Rendered application/_row.html.erb (91.2ms)
Rendered application/_row.html.erb (104.1ms)
Rendered application/_row.html.erb (103.9ms)
Completed 200 OK in 1617ms (Views: 1599.0ms | ActiveRecord: 8.3ms)
Rails 4.2.9 ( config.cache_classes = true )
(previous render messages intentionally removed )
Rendered application/_row.html.erb (2.3ms)
Rendered application/_row.html.erb (2.5ms)
Rendered application/_row.html.erb (2.0ms)
Completed 200 OK in 59ms (Views: 41.8ms | ActiveRecord: 7.7ms
From what I was able to check, when config.cache_classes = false, partials and classes aren't cached by Rails anymore. Not even in the same request like they used to be in 3.2.
I also checked config.reload_classes_only_on_change which seemed pertinent and it's set to true.
Is there something else that I should check/change to get a similar to the one that I used to have in 3.2 with cache_classes disabled? Or is it a regression in Rails that can't be fixed?
Unfortunately developing with cache_classes enabled isn't viable.
Note 1: I created an empty rails 4.2.9 and was able to reproduce this issue by creating a lot of partials and enabling/disabling cache_classes.
Note 2: The same issue seems to happen in Rails 5. I'm focusing in Rails 4 because that's the one that I'm working now.

Rails Scope multi-tenancy on Heroku

I have developed a multi-tenancy Rails app using the technique from Railscast #388 Multitenancy with Scopes.
It works great on my local iMac using POW.
But, I haven't been able to get it to work on Heroku. When the app starts, I immediately get an error screen.
The error from the logs is:
2013-09-05T14:54:43.374240+00:00 app[web.1]: /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/relation/finder_methods.rb:310:in `find_with_ids': Couldn't find Tenant without an ID (ActiveRecord::RecordNotFound)
This is the application_controller.rb code:
around_filter :scope_current_tenant
private
def current_tenant
Tenant.find_by_subdomain! request.subdomain
end
helper_method :current_tenant
def scope_current_tenant
Tenant.current_id = current_tenant.id
yield
ensure
Tenant.current_id = nil
end
I have the domain urls working correctly. But, just in case, I also tried changing the code to this (to force it to a specific Tenant). This also works fine on my local iMac:
def current_tenant
Tenant.find_by_subdomain! 'ame'
end
My main problem is that I have no idea how to debug this.
Thanks for your help!
UPDATE 1
I get the following from the log when I run local:
10:31:05 web.1 | Processing by HomeController#index as HTML
10:31:05 web.1 | Creating scope :page. Overwriting existing method Tenant.page.
10:31:05 web.1 | Tenant Load (0.7ms) SELECT "tenants".* FROM "tenants" WHERE "tenants"."subdomain" = 'ame' LIMIT 1
10:31:05 web.1 | Completed 401 Unauthorized in 75ms
Consider using ActsAsTenant gem. It provides a scoped based approach to multi-tenant applications. It allows flexibility in assigning the tenant within each request, aims to ensure all tenant dependent models include a tenant and can ensure attribute uniqueness within a tenant. Everything that Railscast #388 includes plus more.
The gem works on Heroku without issue..

ActiveRecord banchmark in console

I'm looking at console of with working rails application.
Every request/response finished with next line
Completed 200 OK in 1169ms (Views: 74.1ms | ActiveRecord: 0.0ms)
The problem is that ActiveRecord always show 0.0 and it's wrong, how to fix this ?
the rails logging is not very precise. you might want to have a look at time_bandits for more detailed info: https://github.com/skaes/time_bandits

How to use CouchRest with Sunspot?

I have a problem with integration between CouchRest and Sunspot. When I search the book detail, the result from Sunspot is empty. I try to google it for a long time but no help.
Started GET "/books/search?utf8=%E2%9C%93&query=Book of Life&commit=Search%21" for 127.0.0.1 at 2011-09-08 11:27:41 +0700
Processing by BooksController#search as HTML
Parameters: {"utf8"=>"?", "query"=>"Book of Life", "commit"=>"Search!"}
Rendered books/index.html.erb within layouts/application (10.7ms)
Completed 200 OK in 145ms (Views: 20.6ms | ActiveRecord: 0.0ms)
[] <-- I got empty result
My System
Ruby 1.9.2p290
Rails 3.0.10
CouchDB 1.1.0
File structure ( https://gist.github.com/1164637/ )
Model (/app/models/book_detail.rb)
Controller (/app/controllers/books_controller.rb)
Sunspot Adapter for CouchRest (/config/initializers/couchdb.rb)
Sunspot Adapter Module (/config/initializers/sunspot_couch.rb)
NOTE: Sorry about code link. I always got "Please indent all code by 4 spaces using the code toolbar button". I try to remove all tab and follow SO code formatting guideline but it not work anymore.
Forgive me if I'm missing something, but I can't see how Sunspot is mapping "keywords" to the searchable fields on your CouchRest objects.
To debug first I'd visit Couch in the browser admin UI to make sure my that end is working. Then I'd double check that sunspot is getting anything. If sunspot contains your records then the bug is on the search side, if it is empty that maybe something is up with the object lifecycle management code it injects into your model class.
It's been ages since I did any serious Ruby, wish I could be more helpful. One option is to take advantage of some of the direct CouchDB full text offerings like CouchDB Lucene: https://github.com/rnewson/couchdb-lucene

rails 3 app in production raises 500 error when record not found?

When I go to a bogus url like:
/posts/99999999
On my local machine, I see:
ActiveRecord::RecordNotFound in Posts#show
Showing /Users/patrick/rails/my_app/app/views/posts/show.html.haml where line #1 raised:
Couldn't find Post with ID=99999
... The log shows:
Rendered posts/show.html.haml within layouts/application (29.6ms)
Completed in 423ms
ActionView::Template::Error (Couldn't find Post with ID=99999)
--- However, when I do this in production, I get the public/500.html error page, not 404... I'm kind of confused because in development mode, the browser shows ActiveRecord::RecordNotFound (which should mean 404, yes?) but the log shows ActionView::Template::Error-- not one mention of ActiveRecord::RecordNotFound.
So, ultimately, my question is, how can I make this a 404 not a 500? And-- why is it a 500?
From what I have seen, Rails should automatically make an ActiveRecord::RecordNotFound render the 404.html page... The problem I discovered was I am using the gem decent_exposure, and because of it's nature of lazy loading, it causes the error to happen in the view rather than the controller, and therefore Rails doesn't know to render the 404 page because it shows up as an actual ActionView::Template::Error...
Just add a ! to the find_by_column method.
Example:
#post = Post.find_by_id!(params[:id])
Then, a RecordNotFound exception is thrown.

Resources