Ruby on Rails memory leak when looping through large number of records; find_each doesn’t help

I was able to figure this out myself. There are two places to change. First, disable IdentityMap. In config/application.rb config.active_record.identity_map = false Second, use uncached to wrap up the loop class MemoryTestController < ApplicationController def go ActiveRecord::Base.uncached do Person.find_each do |person| # whatever operation end end end end Now my memory use is under control. … Read more

ActiveRecord::AdapterNotSpecified database configuration does not specify adapter

For you app to work locally you need to: Install Postgresql on your machine Create a database for your development needs (let’s call it my_app_development) Change your database.yml to: default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see rails configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling pool: 5 development: <<: *default database: my_app_development run … 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-3.2.22.5 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

GroupingError: ERROR: column must appear in the GROUP BY clause or be used in an aggregate function

You are not allowed to select reviews.id (selected implicitly through the wildcard *) without adding it to the GROUP BY clause or applying an aggregate function like avg(). The solution is to do one of the following: Remove the wildcard * from your select Add the field reviews.id to your group clause Select reviews.id explicitly … Read more

Rails Models: how would you create a pre-defined set of attributes?

I assume that you are going to have more than a few of these multiple-choice attributes, and would like to keep things tidy. I would recommend the store it in the database approach only if you want to modify the choices at runtime, otherwise it would quickly become a performance hit; If a model has … Read more

Validation to ensure uniqueness of but ignoring empty values?

Yes, there are two possible options that you can pass to validations for optional fields: :allow_blank or :allow_nil, which will skip the validations on blank and nil fields, respectively. If you change your validation to the following, you should get the behaviour you want: validates_uniqueness_of :acronym, :allow_blank => true, :scope => [:group_id], :case_sensitive => false

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)