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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
18 views

Rails Multiple join query

I have the below Model structure class Transaction belongs_to :company belongs_to :user end class Company belongs_to :address end class User belongs_to :address end classAddress has_many :...
Suganya Selvarajan's user avatar
0 votes
0 answers
42 views

Filtering Users with Associated Records by Specific Date in Rails

I'm facing a challenge in a Ruby on Rails application where I need to filter users along with their associated records (Program and Learning), based on a specific date range, but only include those ...
IlyaKokorev's user avatar
0 votes
0 answers
19 views

Transient attributes removed in after_save callback in ActiveRecord model

How can I use a transient attribute during the after_save callback in Rails ActiveRecord class? This is what I am doing right now. I have a model class with multiple attributes that are backed by the ...
Amriteya's user avatar
  • 1,126
1 vote
1 answer
31 views

How to have a PostgreSQL function backing a Rails model?

I have an app written in Rails and a PostgreSQL database. My app has some complex queries that I use to wrap in PostgreSQL views so that I can "plug" Rails models to them - making things ...
Cristiano Mendonça's user avatar
0 votes
1 answer
66 views

ActiveRecord serialize coder argument not seeming to work

I have an Entry class that has a Charge attribute that I want to instantiate from a string as a Charge object when read from the DB: class Entry < ActiveRecord::Base serialize :charge, Charge end ...
Daniel Doherty's user avatar
0 votes
2 answers
52 views

Is there a way to create a Ruby model scope for ordering books? (none numeric, none alphabetic)

I have an app where there are quotes from books with notes or thoughts on the quotes. I want to list out the quotes in order of the book order, chapter order and then page order. Is there a way to ...
CWarrington's user avatar
0 votes
1 answer
25 views

How to specify the actual foreign key in a parent-child relationship when the parent is defined via STI in Rails 5.2?

I use STI in my application, so that Procedure and DataProcess models both inheritate from the BusinessProcess class: class BusinessProcess < ApplicationRecord end class Procedure < ...
user1185081's user avatar
  • 1,976
0 votes
0 answers
31 views

Cannot reference records two levels lower with delegated_type directly

I designed the following model using STI in a Rails (Rails 7.0.8, Ruby 3.2.2) app. class Foo < ApplicationRecord belongs_to :user delegated_type :fooable, types: %w[Bar] end module Fooable ...
nrmn0006's user avatar
0 votes
0 answers
41 views

Accessing a Match record, which belongs to a Run record, which belongs to a User record

I am trying to set up a little Rails app where the intention is basically to pair runners together that want to go on a run at a similar date/time, with a similar distance and pace. So far my app has ...
JdL's user avatar
  • 1
1 vote
1 answer
115 views

In Rails how I add a "helper" method to an attribute / column? [closed]

Let's say we have an table called "Items" and a column named "name" and one named "quantity". Then let's say we have an instance of item like this: @item = Item.first On ...
CafeHey's user avatar
  • 5,801
0 votes
1 answer
77 views

Rails both way relationship between two tables

In Ruby on Rails I'm trying to be able to create both way many-to-many relationship between two tables. I want a relationship in routes and controllers. For example I want to be able to do both of the ...
Abenezer Daniel's user avatar
0 votes
0 answers
27 views

Keep nested attribute's values to show validation errors on form submit

I have a model Parent class Parent < ApplicationRecord has_many :jobs accepts_nested_attributes_for :jobs, allow_destroy: true end and model Job class Job < ApplicationRecord belongs_to :...
Imanpal Singh's user avatar
-1 votes
0 answers
46 views

Optimizing ElasticSearch Integration with Rails

Context: I'm currently enhancing the ElasticSearch integration in my Rails application, dealing with a complex data model involving: Potgres database ORM around 100k data Around 50 associated tables ...
JIGME's user avatar
  • 21
0 votes
1 answer
45 views

Ruby on Rails: How do you create a `has_one` relationship with duplicating primary keys?

I have 2 Tables: Dogs - id (primary key) Cats - id (primary key) I would like to introduce a third table let's say Collars, that both dogs and cats have: Collars: - id (primary key) - size ...
Wallace's user avatar
  • 107
1 vote
1 answer
42 views

Return ActiveRecord Relation from a raw SQL query using parametrized queries to speed up response

I have a Rails model called VideoEventBundle which contains individual events. This is associated with a VideoMeasurement model via foreign key, and the VideoMeasurement model itself is one of the ...
slhck's user avatar
  • 37k
0 votes
0 answers
31 views

Rails Modeling And/Or Logic in a database model with Acts As Taggable On

I am trying to model logic that would allow a user defined search based on boolean logic to return a certain subset of TaggedModel. Below is the code. It has not been tested and probably has typos as ...
Romuloux's user avatar
  • 1,044
1 vote
2 answers
57 views

Query where Date.parse Rails

I'm using a calendar gem which requires event.start_time be recorded as "2023-11-07". I need to find all events in this month and last month based on the start_time, and in which event....
daveasdf_2's user avatar
1 vote
1 answer
79 views

Active record check if rich text is present

I have user model which has description as rich text has_rich_text :description I want to run a query to check which users have a description present? Something like this User.where.not(description: ...
Manav's user avatar
  • 631
0 votes
2 answers
65 views

has_one from model attributes instead of foreign key

I would like to have a has_one relation based on my Product's attributes rather than its id. class Pricing belongs_to :type belongs_to :brand validates :type_id, uniqueness: { scope: :brand_id } ...
Daniel Costa's user avatar
0 votes
2 answers
77 views

Ordering by attribute that contains letters and numbers

We need to order a list of room numbers. You can think of the units being numerically ordered, with the possibility of a letter in front as a prefix, e.g 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, A1, A2, A3, A4, ...
VegaStudios's user avatar
0 votes
2 answers
42 views

Query db for a model including related_model, where related model have 2 specific ids

In a rails app, i have a 3 models, Room, User and UsersRoom which is an association table between Room and User. I want to query for a Room related to multiple users. If i do: Room.includes(:...
Sonfaya's user avatar
  • 37
1 vote
4 answers
114 views

Custom has_one based on column value

New to Rails, was wondering if its possible to model this relationship with ActiveRecords: I have the following models (simplified): User: - name - email Client: - name - address And then I ...
João Menighin's user avatar
1 vote
2 answers
105 views

Rails Active Record Object being created for no reason

I'm using Rails 7. I have the models Post, Venue and VenuePost models. They look as follows: class Post < ApplicationRecord has_many :venue_posts, dependent: :destroy ...
User12234327832's user avatar
0 votes
1 answer
22 views

is there a way to get back the where conditions in an ActiveRecord::Relation?

If I do: rel = Foo.where(:foo_field => 123).joins(:bars).where(:bars => { :xyz => 123 }); Is there a way to get back the bars conditions { :xyz => 123 } from that relation? I see that I ...
patrick's user avatar
  • 9,384
3 votes
1 answer
85 views

Fixing DEPRECATION WARNING: Calling ActiveRecord::Base.clear_active_connections

I'm working on a Ruby Sinatra app with Active Record and it gets this error message printed in the console with every HTTP call. I couldn't find anywhere more information about this deprecation or how ...
Alvaro's user avatar
  • 41
0 votes
1 answer
75 views

is there a way to make ActiveRecord relations use aliases?

If I have a models like class MyTable < ActiveRecord::Base has_many :other_tables scope :my_scope, -> { select('t.id') .from('my_tables t') .where(...stuff) ....
patrick's user avatar
  • 9,384
0 votes
1 answer
24 views

Why isn't order() taking effect on papertrail version associations?

I am using papertrail gem to track versions of my Problem and Outcome models. I can successfully order Papertrail::Versions like so: PaperTrail::Version.order(created_at: :desc).limit(1).select(:id) ...
digitalWestie's user avatar
0 votes
2 answers
51 views

Joining two models with many-to-many relationship in Rails

I have a ruby on rails api which has a many to many relationship between the following models: class Course < ApplicationRecord has_many :student_courses has_many :students, through: :...
Ishmael7's user avatar
  • 174
0 votes
0 answers
42 views

Overwrite rake tasks in sinatra project and activerecord

I have a Sinatra fun project and I want to improve my rake tasks so that everytime I create a new migration and migrate it I don't have to manually run them with RACK_ENV=test bundle exec rake db:...
jedi's user avatar
  • 2,021
0 votes
0 answers
14 views

RoR ActiveRecord -- What happens behind the scenes if you try to do x.save if x came from a read replica

I just migrated to rails 6, and am testing the new multi db functionality. I have a remote read db running in aurora, and a write database locally to simulate a read and write cluster. I am pulling a ...
wertyuio's user avatar
0 votes
1 answer
48 views

How to rename column in rails through terminal?

I am unable to rename column through terminal how to rename column in rails without writing anything inside migration file .I meant to say how we can rename column through terminal only.so please give ...
Govind Patidar's user avatar
0 votes
1 answer
70 views

ActiveRecord 7: using writing and reading roles without ORM model classes

I'm working on an application that uses ActiveRecord for database access, but does not use ORM features such as model classes - queries are all made with literal SQL and calls to exec_query. I'd like ...
Douglas Greenshields's user avatar
0 votes
0 answers
63 views

How can I create multiple images of the same application in Docker with Multiple dynamic Database?

I am using rails 5.1.0 I want to create multiple images of the same application, The domain and database of each image will be different, Example for image_1 Domain example.app.com Database - ...
Rakesh Bakoriya's user avatar
1 vote
2 answers
43 views

Rails ActiveRecord has_many assocation with same model

I'm working on implementing product bundles in an existing site. I have a Product model with various product types in an enum that I have added a product type of "bundle" to. I want to be ...
Kenn's user avatar
  • 189
0 votes
1 answer
47 views

Mapping Models/DB to an external API, what is recommended design?

What would be the best way to handle mapping Laravel models to an external API? The external API has entities such as: user posts comments On my local database, I also have similar entities: user ...
Bernard Wiesner's user avatar
0 votes
0 answers
37 views

I'm looking for a better way to figure out the root of this ActiveRecord::StatementInvalid problem I'm having

I'm very new to coding so please bare with me being a noob. Ive got a wallpaper calculator that works fine in both development and production environments on localhost:3000, but when I deploy the site ...
WyattFantana's user avatar
0 votes
0 answers
10 views

Activerecord select and distinct

What is the preferable way to use distinct with select in rails? Model.select(:column).distinct OR Model.select('DISTINCT column') I checked and the sql generated by both is same, so can they be ...
Ashu Chauhan's user avatar
0 votes
1 answer
62 views

My Ruby on Rails active record validation error does not persist when I re-render the same view

I'm using Rails 7 and Carrierwave to upload images. My image uploading is working as expected, but my issue is happening when I try to implement validation error message for an incorrect file type. In ...
Tony M's user avatar
  • 341
0 votes
0 answers
73 views

Rails 5.1 .any? and .exists? causes unknown column

I'm working with Rails and have encountered an issue with a model that references a table defined in schema.rb as: create_table "test_table", primary_key: ["somthing", "...
sneakyfishies's user avatar
1 vote
2 answers
87 views

How to handle static master data as models in Rails

Is it possible to treat non-database master data, such as the ones in config/master_data/something.yml, as Rails models (like Active Record)? I'd like to have functionalities such as fetching an item ...
speed_star777's user avatar
0 votes
1 answer
41 views

Is there a replacement for ActiveRecord `source` in ecto?

Please help me in finding the elixir/ecto option for :source option of has_one/has_many :through association of ActiveRecord. Here is my problem, I have the following schemas: User, Event, ...
Linda Kadz's user avatar
0 votes
0 answers
42 views

Return all instances based on the max of an attribute of an associated model

Realise the title isn't well put at all. Essentially I have a model TaskReferral that has a has_many relationship with TimeSlot. I want to return all instances of TaskReferral that do not have any ...
DavidM's user avatar
  • 173
0 votes
1 answer
45 views

Rails validation on insert vs create

I have a model where the validity of a record is dependant on what other records exist at the time of insertion. Whilst not exactly the same scenario, let's imagine I have an AuctionBid model - a bid ...
Neil Middleton's user avatar
1 vote
1 answer
44 views

How to fetch fetch one column lazily with a jOOQ active record

I have a PostgreSQL table that defines a couple of small simple columns and one column data that stores large binary data: create table my_table ( id serial primary key, data ...
Leprechaun's user avatar
3 votes
0 answers
137 views

has_many association .build is not saving object in DB for Rails 6.1

I'm upgrading to Rails 6.1, and have the following associations in my model class Product < ApplicationRecord has_many :classifications, dependent: :destroy has_many :order_lines, dependent: :...
Faisal's user avatar
  • 18.9k
0 votes
2 answers
43 views

How can I avoid unnecessary loading of Associated model while creating it?

In my Rails app, we have a couple of models similar to this class Pen < ActiveRecord::Base has_one :ink after_create :add_ink def add_ink create_ink(color: "blue") end end ...
jwilm's user avatar
  • 457
0 votes
0 answers
7 views

Active Record Callbacks Order of Exectution

why around_save is called earlier than before_create although around save contain the logic before and after saving the record so one the record is saved then why there is a need to call before_create ...
Farooq Haider Alvi's user avatar
0 votes
0 answers
78 views

Forcing a delegated type in Rails `has_many`

Let's say I have these models similar to the DelegatedType docs: # Schema: entries[ id, title, created_at, updated_at, entryable_type, entryable_id ] class Entry < ApplicationRecord ...
DEfusion's user avatar
  • 5,543
0 votes
1 answer
42 views

How can I dump an ActiveModel record as an INSERT SQL statement?

Previously with Rails 6 I have been doing: # I have an existing record record = MyRandomModel.take # Now I wanna get the INSERT SQL which would have been generated and executed on the DB ...
Niels Kristian's user avatar
0 votes
2 answers
62 views

Rails 7 many-to-many relationship, search for items with multiple related items

Assuming I have three models, some items, identifiers and a joining model in Rails 7 class Item < ApplicationRecord has_many :item_identifiers has_many :identifiers, through: :item_identifiers ...
Shuttleu's user avatar
  • 123

1
2 3 4 5
573