I have a couple simple queries and I'm not sure what the best practice is. I've written them in 2 ways which produce the same result. Which is preferred? Or i
I need to get the last record with a certain value, but without loading it. I have to do something like: Thing.where(cool: true).where(created_at: Thing.where(c
I have a very large table Metrics with a primary integer key named id. The primary key is 32 bit integer and is about to overflow. My attempted solution to this
I have a legacy database where I have two models as: class Purchase belongs_to :product end class Product has_many :purchases end Now over time some of
I'm trying to add a button to my page that exports a list of the invited users with Devise Invitable. I want to include the invitation_tokens in that list. User
To get a single random record from the db, I'm currently doing: User.all.sample But when there are 100000+ users, it takes a few seconds to load them all, jus
I am making a shopping cart for a web app. So far it has 3 components: 'products', 'line_items' and 'carts'. The flow seems to be okay. I am getting all the
Can add_foreign_key add a 'NOT VALID' parameter to the ALTER TABLE command? (Postgres, if it mattters) I have a foreign key between two very large tables. I ne
The Ruby on Rails and ActiveRecord documentation, Google, and StackOverflow are conspiratorially silent on the return value of update_all() What does update_al
I am upgrading an application from Ruby 2.6.10 to Ruby 3.0.4 and have a very odd situation. When I execute the command record.save(validate: false) on one of m
I inherited a big, complicated Arel query that pulled from a number of tables. A new requirement says that if one of those tabels doesn't have a value for a par
I have a business requirement to select records based on two fields in one table: code1 and code2. The selection is complex and hard-coded, with no codeable rhy
I want a nested transaction to fail the parent transaction. Lets say I have the following model class Task < ApplicationRecord def change_status(status,
I know that Model.group(:account_id).count will return a hash of account_ids => counts. {3=>3, 4=>3, 8=>8, 10=>5, 20=>4} I'd like to retur
I have a Release model with medium and country columns (among others). There should not be releases that share identical medium/country combinations. How would
How do I use the created_at field to get only the records that were created today and no other day or time? I was thinking of a ->where('created_at', '>=
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
I want to query an ActiveRecord model, modify it, and calculate the size of the new object in mb. How do I do this?
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
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