Questions tagged [activerecord]
Active Record is a pattern that combines domain logic with storage abstraction in single object. Use this tag for questions about the pattern, [rails-activerecord] for questions about the Rails ORM framework. Do not use this tag for [codeigniter] questions, instead use [query-builder].
activerecord
28,852 questions
3
votes
0
answers
164
views
When eager-loading an association with includes(), is it possible to use named scope to avoid writing long & complex Hash?
Let's say I have this sample model structure:
...
0
votes
1
answer
90
views
Rails Nested Attributes Not Persisting
OK, the basis of the issue is a set of nested attributes is not persisting. The only oddity is that the attribute can be delegated to a delegate_parent. I don't think this should cause any issues, but ...
1
vote
1
answer
165
views
Why does saving fail with a mutual belongs_to association in ActiveRecord?
This code may be difficult to understand at first glance, but it was created solely to reproduce an unusual behavior and does not represent actual application code.
Therefore, comments such as “the ...
0
votes
0
answers
127
views
Rails 7.2 Composite Primary Keys with `id` column
I'm working on upgrading our Rails app from 7.1 to 7.2, and one of the changes is better support for PostgreSQL's composite primary keys. One of our models uses this feature, with the following line ...
0
votes
1
answer
96
views
use activerecord-import to upsert only when values have changed
How to use activerecord-import to upsert only when values have changed?
Body:
I'm using the activerecord-import gem in a Rails 6 app to bulk upsert data into a PostgreSQL database. Here's an example ...
0
votes
1
answer
61
views
Is there a way to skip the voter_memberships association and adjust the voters association so it keeps working
I have 3 models where all of the associations work. I can do the duplicated voter_memberships/voters for contributor, editor, and manager, but I'd like it to be one association per role and not two.
<...
0
votes
1
answer
95
views
SELECT clause in php-activeRecord not working
I have this query without SELECT clause
...
0
votes
1
answer
145
views
Rails 7.2 PG::InvalidTextRepresentation with integer column & binding
We are planning the migration to Rails 7.2.2.1 (we currently run on 7.1.5.1).
We have noticed some errors during the CI run while querying with binding variable on integer column.
For example :
...
0
votes
0
answers
55
views
How to use lockbox and money-rails gems together to encrypt a monetary field?
I have an ActiveRecord model with a monetary field I need to encrypt. I use gems lockbox and money-rails in this application for ...
2
votes
2
answers
176
views
Rails 8, postgres, distinct on and ordering
This feels so much harder than I think it should!
I have a Rails model: MarketplaceMessage
Every message has an initial_record_id...
0
votes
2
answers
112
views
PostgreSQL: there is no unique or exclusion constraint matching the ON CONFLICT specification
I get the following error on a query with PostgreSQL 16.8 on x86_64. It works fine on the other tables with the same query and unique index. I tried recreating the index multiple times, but no luck. ...
1
vote
1
answer
75
views
Active Record counting group from association
Can I get the same output of the SQL query below using ActiveRecord?
...
0
votes
1
answer
233
views
ActiveModel custom attribute type with option - automatic type validator?
I have some models that store the params of a user database query. They are not ActiveRecord database-backed models, more like what you might create with ActiveModel to compile and validate form ...
0
votes
0
answers
51
views
Rails 7.1.5 `touch: true` causes StackLevelTooDeep with `delegated_type`
In a Rails app for managing course bookings, I want to keep the updated_at timestamps of related models in sync.
Here's the setup:
...
1
vote
1
answer
103
views
Rails ActiveSupport::Concern — scope can't call private class method
I'm trying to refactor a scope within a Concern so that the scope gets most of its logic by calling different class methods, depending on an arg.
With "public" class methods, that works fine....
0
votes
0
answers
204
views
Creating a partitioned table with Rails 7.2 and Postgres
I need to create a Postgres table partitioned by range. The partitioning approach is essential because the table will need to store large volumes of data and be queried frequently. The plan is to set ...
1
vote
1
answer
68
views
Get record if search term is in one of its related tables related tables in Active Record
Apologies of the title is convoluted but thats basically what I want to do. I have a Table, Building, that is Related to a Table Named Residents, and those Residents can have many Photos and other ...
-1
votes
1
answer
94
views
Rails 5.2 Monkey Patch ActiveRecord:Inheritance ClassMethods
Im trying to Monkey Patch the follwing ActiveRecord Model, But im having trouble getting it to stick. Im trying to prepend my module so that it overrides the Rails implementation of a private method ...
3
votes
3
answers
305
views
Remove null bytes for every ActiveRecord string attributes
I have a Rails application using Postgres as the database.
Postrgres does not accept strings containing null bytes (example: "a \u0000 b"). Trying to save ...
0
votes
0
answers
41
views
Prevent N+1 queries with custom select in association
I include an association with additional attribute projects_count. Even with the includes method, I still got N+1 queries for ...
0
votes
1
answer
67
views
Custom method as ID
I have two models:
...
0
votes
0
answers
51
views
Padrino framework with activerecord doesn't create schema file?
I have padrino version 0.15.3 and activerecord 7.2.I use task:
padrino rake ar:migrate
Errors:
undefined method `schema_format' for ActiveRecord::Base:Class Did ...
2
votes
1
answer
130
views
Whats the difference between `joins` and `references` in Rails?
What is the difference between this:
User.includes(:sites).where("sites.name = 'test'").references(:sites)
and this:
...
1
vote
2
answers
241
views
Undefined method `create_table' for ActiveRecord::SchemaMigration:Class (NoMethodError)
When I create Padrino framework with SQLite3 and activerecord.
padrino rake ar:migrate
@schema_migration.create_table
Or when:
padrino rake ar:schema:dump
<...
0
votes
0
answers
51
views
ActiveRecord - copy data from another database (without models)
I'm trying to connect to an old database to copy/process data to the current database (by Rails environment) (both sqlite). I want to execute raw SQL on the old one and create models in the new one, ...
0
votes
0
answers
140
views
How to Access Rails App's `ActiveRecord::Base.connection` in a Gem?
I am developing a Ruby gem that will be used by a Rails application. In it, I would like to use database connection (ActiveRecord::Base.connection) that the Rails app itself uses.
The gem will be ...
-1
votes
1
answer
165
views
ActiveRecord not getting id after save
I got my application to weird state - when I create record (in application or in console), it is created in database, object reports to be persisted, but the object has no id (...
0
votes
2
answers
101
views
Rails: Active Record Validation for has_one
I have 2 models
class A < ApplicationRecord
has_one :b, dependent: :destroy
has_one :c, dependent: :destroy
end
and
...
1
vote
1
answer
232
views
PgVector Active Record support
I was successfully added a vector field on my database
...
0
votes
2
answers
119
views
Rails delete record from joins table: delete_all fails when using additional joins
Happens both at Rails V 5.2.8.1 and Rails 7.1.3.2
My models:
...
0
votes
1
answer
73
views
Query on JSON object keys in ActiveRecord/Postgresql
I have a json column in an Answer table in a rails app; document. I'm trying to write a scope which returns true if the ...
0
votes
0
answers
55
views
Get version of model before it was saved?
I have a Sidekiq Job that syncs one of my models to an external source. This is a - let's say expensive - operation, that I would like to run only when needed. Reading the current state of the ...
0
votes
0
answers
71
views
Rails has_one through one of multiple objects
Is there a cleaner way of associating a has_one...through relationship if the original object can have one and only one relationship through a couple of different ...
0
votes
1
answer
59
views
Uploaded PDF Not Downloading In Rails
I have quite a weird situation here in my Rails App.
I have an API call for uploading documents, namely PDFs, which get attached to a Document object via ...
1
vote
1
answer
146
views
0
votes
0
answers
54
views
Rails activerecord-import not work with unique key
model
class User < ApplicationRecord
validates :name, uniqueness: true
end
schema
...
0
votes
0
answers
61
views
Search results undefined while loading from controller on Rails view
I am trying to do search functionality for my Rails MVC learning. I am adding title , description and category name as filter columns. And checking each parameter id defined in my model class using ...
1
vote
1
answer
89
views
Why does Rails use the prefix 2:: in the session_id stored in the database?
I am working on a Rails application that uses ActiveRecord::SessionStore for session management. While debugging, I noticed an interesting behavior regarding the session_id. Here's what I found:
The ...
-2
votes
1
answer
54
views
Yii2 Active Record Order By Primary Key ID not working [closed]
I have a pretty basic Yii2 ActiveRecord query.
$paperList = Paper::find()->orderBy(['id' => SORT_DESC])->all();
id is my primary key column which is of ...
0
votes
1
answer
47
views
How to query records by matching a parameter within the same record?
In my Rails application, I have a Project model with an associated Task model (Project has many tasks). Each Task has a field called parent_task_id that links it to a task from a previous version. ...
0
votes
0
answers
68
views
Rails: Proper convention for many one-to-one relationships in a single rails model
I have a "central" model that is referenced by many other models like this:
...
0
votes
1
answer
103
views
Relationship problem: there is no unique constraint matching given keys for referenced table
I'm trying to implement partitioning in PostgreSQL. The problem is that one partitioned table references another partitioned table. There is also a third regular table that also references the ...
1
vote
1
answer
122
views
No connection pool for 'ActiveRecord::Base' found for the ' ' role
I upgraded our rails application from version 6.0 directly to 7.0. After commenting out the new framework defaults everything seemed to work smoothly but after setting the load_defaults to 7.0 I got ...
0
votes
0
answers
58
views
How to save image with Paperclip for specific model record [duplicate]
I have a model where each record has an image with a :background field that is used on the frontend. Paperclip is already used in the project, and in the admin ...
2
votes
1
answer
102
views
Nested SQL transactions in Active Record - risks?
I am in a situation in which I am forced to use nested transactions:
I have several methods that save a bunch data, each of them needs to be wrapped in a SQL transaction, and they also have to call ...
0
votes
1
answer
398
views
Binding Parameters with Rails ActiveRecord::Base.connection.exec_query in mysql
I am attempting to utilize binding parameters in Rails using ActiveRecord::Base.connection.exec_query for a mysql database, but ...
0
votes
1
answer
58
views
Rails, how to lazily replace an ActiveRecord collection
I am writting a method that reassigns a polymorphic collection like:
...
0
votes
0
answers
84
views
Filter records based on enum array column in ActiveRecords Rails
class ModelName < ApplicationRecord
...
end
For example, purpose "ModelName" has a column name 'example_column' with type of enum array. Let's ...
0
votes
0
answers
55
views
How to handle nested association in Rails
I have two models in Rails:
...
2
votes
0
answers
72
views
Multiple duplicated prepared statements using Rails + MySQL
We recently hit a limit of prepared statements in our MySQL DB, and after some digging I found that we are creating a really high amount of duplicates for the same prepared statement. For example:
<...