Category "activerecord"

Best practice for similar repeated queries using ActiveRecord

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

ActiveRecord Get Max Value Without Loading

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

How do I make ActiveRecord search multiple tables (of similar schema) by default?

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

Find records with missing associated records in Rails

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

Devise Invitable: Return a list of invitation tokens

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

Rails—get a random record from db?

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

Ruby on Rails // Getting a Strange Return on .each loop

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

Rails ActiveRecord migration to add foreign key with 'NOT VALID' parameter

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

What is the return value of update_all() in ActiveRecord / Ruby on Rails?

The Ruby on Rails and ActiveRecord documentation, Google, and StackOverflow are conspiratorially silent on the return value of update_all() What does update_al

Ruby 3 - Save without validation no longer works on one table

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

Arel - How to coalesce a field and a string literal in an Arel query?

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

Rails SQL "select in" across several columns: where (code1, code2) in (("A", 1), ("A", 3), ("Q", 9))

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

Rollback entire transaction within nested transaction

I want a nested transaction to fail the parent transaction. Lets say I have the following model class Task < ApplicationRecord def change_status(status,

Rails Query group and pluck to Return Array of IDs grouped by another attribute?

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

Rails: validate uniqueness of two columns (together)

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

Get only records created today in laravel

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', '>=

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