Dynamic error pages in Rails 3

In rails 3.2, it’s easier than that: Add this to config/application.rb: config.exceptions_app = self.routes That causes errors to be routed via the router. Then you just add to config/routes.rb: match “/404”, :to => “errors#not_found” I got this info from item #3 on the blog post “My five favorite hidden features in Rails 3.2” by By … Read more

Change the default value for table column with migration

You have to check which version of ActiveRecord you are using. According to your command rake db:migrate you are still on Ruby on Rails 4.2 or earlier. If you are on ActiveRecord up to 4.2 (change_column_default 4.2.9), there is no from/to option and you can define only the new default option as param. class ChangeDefaultvalueForHideSeasonSelector … Read more

PG::InvalidParameterValue: ERROR: invalid value for parameter “client_min_messages”: “panic”

To make it work with PostgreSQL version 12, I monkey patched PostgreSQLAdapter class to replace ‘panic’ with ‘warning’ message. Note, if you can upgrade activerecord gem to 4.2.6 or higher versions you don’t need to have this monkey patch. I had to do this because my project depends on gem activerecord- require ‘active_record/connection_adapters/postgresql_adapter’ class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter … Read more

Build hash from collection of ActiveRecord Models

Here are some one-liner alternatives: # Ruby 2.1+ name_to_code = countries.map{ |c| [c.name,c.code] }.to_h # Ruby 1.8.7+ name_to_code = Hash[ countries.map{ |c| [c.name,c.code] } ] # Ruby 1.8.6+ name_to_code = Hash[ *countries.map{ |c| [c.name,c.code] }.flatten ] # Ruby 1.9+ name_to_code = {}.tap{ |h| countries.each{ |c| h[c.name] = c.code } } # Ruby 1.9+ name_to_code = … Read more