Questions tagged [activerecord]

Active Record is a powerful design pattern that seamlessly integrates domain logic and storage abstraction into one cohesive object. Any inquiries regarding this innovative pattern can be directed here, while questions specifically about the Rails ORM framework should be tagged with [rails-activerecord].

Truncating Rails bigint with as_json

When models have bigint columns with values such as 1456299399553483799, they often get converted to slightly different numbers like 1456299399553483800 when using the as_json method. Is there a simple or predefined method to automatically convert these b ...

Trying to optimize this Active Record Query for better performance in Ruby on Rails

Hello, I'm currently new to active record and exploring two map functions. I'm curious if there's a more efficient way to optimize the following query: When I run this query, I see hundreds of lines in my terminal and worry about potential memory issues i ...

Improving the Efficiency of Database Queries in Ruby on Rails

Currently, my team and I are collaborating on a Rails side project that involves loading a substantial amount of seed data. This data is stored in a JSON document with nearly 3200 JSON objects, each having the same three field names. After parsing and see ...

Safely handling variable table names in MySQL with Codeigniter

Currently, I am encountering a minor issue as I work on an application that necessitates the use of dynamic table names in MySQL. Essentially, my process involves selecting albums from a database based on various factors such as Genre, Play Length, and Re ...

How can I implement Active Record with this SQL?

Currently utilizing CodeIgniter with MySQL 5 for my database. The SQL query below is working fine, but I am concerned about its compatibility with MSSQL, PG, and other databases. Is it possible to convert this statement into something that can work across ...

Sort ActiveRecord objects based on JSON type data structure

Recently delving into the world of JSON datatypes and curious if Rails has any clever shortcuts for sorting. Let's say I have the following: object_a.payload = { "category_1" => 2, "category_2" => 1 } object_b.payload = { "category_1" => 1 } obje ...

Using Yii2 Active Record to find records where a field value is divisible

When working in SQL, I was able to select records where their IDs are divisible by 4 using the following query: SELECT id FROM table_name WHERE id%4 = 0; However, when attempting this in Yii2 active record, it resulted in an error: $model = Verses::find() ...

Creating JOIN tables within the create action involves assigning related ids to each table

I am currently working on a room reservation system that involves including options for each room. Data related to the options and their join table, reservation_options, are successfully inserted into the params. However, I am facing an issue with assignin ...

add to a visual representation of a chart in a separate database

Having an issue with yii2 and in need of assistance: I have two databases, db1 and db2, both on MsSQL. In db2, there is a view linked to a table in db1. The problem arises when I try to insert into db2 as I am unable to retrieve the transaction id using ...

Can JSON be extracted from the database?

I have a Ruby on Rails application running on version 5.1 with PostgreSQL version 9.6 as the database. Within one of my tables, I am storing data in a jsonb field. My goal is to simply retrieve this string and send it to the client without having to conver ...