Category "activerecord"

Is it OK to specify a schema in `table_name_prefix`?

TL;DR: Is it OK to specify a schema in table_name_prefix? We have a large Rails application that is not quite a traditional multi-tenant app. We have a hundred

How do I get the size of a ruby object in mb in Rails?

I want to query an ActiveRecord model, modify it, and calculate the size of the new object in mb. How do I do this?

Change the default value for table column with migration

I try to change the default column value from false to true. But when I run rake db:migrate VERSION=904984092840298 I got the following ERROR. StandardError: A

Find rows with multiple duplicate fields with Active Record, Rails & Postgres

What is the best way to find records with duplicate values across multiple columns using Postgres, and Activerecord? I found this solution here: User.find(:al

Query method issue on rails upgrade to 6.1.5

enter image description here Upgraded rails from 6.0.3 to 6.1.5 but having issues with query methods like eager_load and references. class Note < Application

how to check if has_one exists without loading associated model

I have a simple has_one relationship class User < ApplicationRecord has_one :detail has_many :courses end class Detail < ApplicationRecord belongs_

Difference between after_create, after_save and after_commit in rails callbacks

The difference between after_create, after_save and after_commit in Rails is that: after_save is invoked when an object is created and updated after_commit is

is there a way to set after_commit callbacks to a specific ActiveRecord transaction

I am creating a ActiveRecord transaction this way ActiveRecord::Base.transaction do MyModel.create!(name: "value") OtherModel.create!(name: "value") end

Rails: select unique values from a column

I already have a working solution, but I would really like to know why this doesn't work: ratings = Model.select(:rating).uniq ratings.each { |r| puts r.rating

What happens after I add an index to a large table in a production Rails app and then migrate?

Rails 3.2 app running on Heroku with Postgres. I added an index add_index :lines, :event_id Events have_many Lines. There are about about 2 million Lines and

What is the purpose of views' predefinitions in dump file

I'm working on the project where we are using :sql schema format for the Active Record dumper (to support more complex logic like triggers). We have a lot of