Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [active-record]

Active Record is a pattern that combines domain logic with storage abstraction in single object.

0
votes
1answer
30 views

Updating one model field based on the associated model's field

I'm wondering if there is any way we can optimize the following code. Scenario is Library's active field should be ...
2
votes
0answers
55 views

Counting how many questions in each category are tagged

I have a homepage that shows how many questions are in a certain category which is done through tagging. The functionality is working great, it tells me how many of each there are. However, newrelic'...
4
votes
2answers
80 views

A method that shows maps users are a part of

A little background: My app has two models called mapgroups and usergroups. A map belongs ...
0
votes
2answers
37 views

ActiveRecord query for coaches a client has bookmarked

I'm trying to figure out the best way to set up an ActiveRecord query. Models There are three relevant models involved: ...
0
votes
2answers
34 views

Counting number of messages for a mailbox over several periods

The idea is to return a number of messages received today, this week, this month. ...
4
votes
7answers
1k views

Display related records, if they exist, in Rails view

Is this much logic in view is justified to check for the presence of record? ...
1
vote
3answers
499 views

Count has_many relationships

I have 3 models: Import, Product, Values. An import has_many products. A product has_many values. I want to find the product with the highest values count and the sum of all values for all those ...
3
votes
2answers
64 views

Rails method to match users according to language fluency level

This code works fine, but I'm still learning, and feel like there is a more Railsy way to write this. Is there a good rails ActiveRecord method I should learn more about? Please let me know if you ...
3
votes
2answers
313 views

Seed a database

I'm looking for advice / best practice to avoid doing a deeply nested set of IF/THEN statements in Rails. The intention is to ...
2
votes
2answers
223 views

Update single common column in multiple has-many relationships rails

I want to update a single column in 4 tables. All relationships are has-many among them. Private schools has many private classes Private classes has many lesson plans Lesson plans has many quizzes ...
3
votes
2answers
174 views

Build a pizza and choose your toppings

I've created a scenario below, in which a user can build a pizza and choose their toppings, then order their pizza: ...
1
vote
1answer
44 views

Finding users who liked a post

I have a method liked_by and it doesn't seem like appropriate as per Ruby style. I need to do it as less as possible. ...
1
vote
1answer
549 views

Directed Acyclic graph implementation in Ruby on Rails

I have an implementation for a graph node class that I'd like to have function as a directed acyclic graph. The associations are roughly as follows ...
1
vote
0answers
677 views

Converting ActiveRecord array to JSON

I have two tables, devices and device_properties. The devices table columns are as follows: ...
-5
votes
1answer
42 views

Getting last 5 leave requests for a user - where does the code live [closed]

I have a User model, a user has_many leave_requests, in the user model I have the following code to find the last 5 leave requests: ...
2
votes
2answers
2k views

Simple ActiveRecord attributes encryption in Rails 5+

The task at hand was to ensure that certain columns in the database are encrypted in case the database data is stolen. I have a feeling that a lot can be improved here (naming, DSL, encapsulations) ...
5
votes
3answers
75 views

Marking loans as paid if the payments exceed the loan amount

I have a Rails app where Loan has paid attribute. Loan should be marked as paid if total sum of payments is equal or higher than loan amount_to_pay. Problem is that ...
1
vote
1answer
54 views

Rails query for all questions in tournaments that a user participates in

I'm new to rails and this is the situation I'm in: I have a User model that has many tournaments, a Tournament has many users ...
1
vote
2answers
64 views

Fetching items from database with “Outstanding” status

I am fetching around more than 300000 items through a query: ...
3
votes
1answer
152 views

Getting timeslots using a single query

I want to get latest time slots for 2 cases based on doorman true for false based on logged in user. Cases are: If doorman is true, the query will be the same if doorman is false, then I need to ...
3
votes
1answer
47 views

Extracting queries from controller to model

I had a complex query in the controller like: ...
4
votes
1answer
171 views

ActiveRecord selection, modified by optional parameters

I have an action method in my Rails controller which filters an ActiveRecord model depending on the submitted GET parameters. Rubocop complains that Assignment Branch Condition Size is too high: ...
5
votes
1answer
72 views

Displaying information about a ticket purchase

My code works perfectly, however, I know my approach isn't best practice practice. I would appreciate the effort of anyone who tries to explain in details on how I could refactor this code using ...
3
votes
1answer
84 views

SE-like voting system

I've created a voting system that's similar to Stack Exchange's in a Ruby on Rails web app. It's working great - as far as I can tell, all the edge cases are caught. This is the action to cast a vote. ...
2
votes
0answers
311 views

Custom bang! method and Rails validation

I have a model called booking. ...
2
votes
1answer
62 views

Count associations in a Ruby model

I'm trying to figure out if there's a better way to run the code below. Basically Course is an association of Student and ...
2
votes
1answer
84 views

Changing a user status flag, with validation

I am writing a function which on update of any attribute of model sets the status of variable is_kyc_verified to false. Here is the code of the User model and the ...
3
votes
1answer
41 views

Simpler method to find users in child categories

I am using the ancestry gem to organize my post categories tree. I have 8 parent categories, and they all have many child categories. To retrieve an array of child categories, I use: ...
1
vote
0answers
57 views

Generating a Postgres query dealing with creating a time series

I want to sum the number of my published publications model in a given interval and return a time series. (related SO) The main challenge is that I want the code to respect the time zone passed to it....
3
votes
0answers
34 views

Updating ActiveRecord with text from a joined table

I have an Active Record update with the following data model: TvMenuProfile has one rich_text_special ...
3
votes
0answers
114 views

Custom MySQL duplicate_key update queries

The goal is to create an activity or update its counter if it's already present. And to be super accurate, the code avoids race conditions with a custom ...
1
vote
1answer
58 views

Using as few queries as possible for many table associations

I have a table containing about 30k rows of data and there's several table associations. The problem is that there's like 18 columns, and each column requires 1 db query. Here's an example of what I'...
5
votes
2answers
71 views

Selecting users with biopsies in a given country for charting

I have this monstrosity ...
3
votes
1answer
3k views

Managing users and profiles in Rails

I am building a Rails marketplace application using TDD. I would like to get advice on the way in which I have built the User associated ...
2
votes
0answers
56 views

Modeling a temperature recording device in Rails

The app will allow users to login and upload a CSV file that contains temperature data from a device. This data will be stored and the user can view all these downloads and select one from a list to ...
1
vote
0answers
61 views

Copying csv to sql_table

I wrote code that takes csv and export it to sql table. It looks like this: ...
1
vote
1answer
48 views

Checking whether users have a role that allows them to index sheets

Is there any DRYer way to write this method ? or is it better to keep it this way for better reader understanding ... Each role has a list of authorised activities for a specific resource ( in this ...
7
votes
1answer
73 views

Summing the size/count of associations for multiple records

I'm working on a plain Ruby class within a Rails application. For the total_leg_count method---is that the best way to go about achieving what I want? I feel like ...
2
votes
1answer
171 views

Skinny model, fat controller in Rails managing user IP addresses

As part of learning Rails framework, I have small app in progress. Basically idea is, user can create IP address, define is it in use, to which server is assigned, which user is it using and role of ...
3
votes
3answers
48 views

Searching purchase records, with or without a vendor criterion

I'm trying to figure out a better way to have one query here. I want to be able to send something to last where statement a wildcard so I can select all vendors. Right now if I don't include that line ...
2
votes
2answers
67 views

Locating existing records

In order to reduce redundancy in my app, I have added the method self.find_or_keep(object) to many of my models in order to find existing records with the same ...
3
votes
1answer
67 views

Method that reserves a reservable entity and charges the user

This method works for what I need it to do, but I just don't think it's extremely readable, and could be abstracted into different methods. I have a feeling I'm going against some best practices. ...
-4
votes
1answer
54 views

Fetching shipment statuses for a user [closed]

I think the inherent relations in this query should be self-evident: ...
2
votes
1answer
65 views

Reduce the calls to database for validations in rails

I was building a rails web, and used form object to separate the validation logics per form. I had to find_referral_code twice in order to store the user_id of referrer. I wonder how I can improve ...
2
votes
2answers
55 views

Purchase controller search method

I've been trying to simplify a search method in a project that I've taken over. Is it possible to make the code any cleaner than I already have done? purchase_controller#search (original) ...
1
vote
0answers
400 views

Association count in single query for ActiveRecord

This is a small gem that gives the possibility to include counts of reflections when fetching active record objects. What do you think of the code? Any improvements or other (similar) use cases where ...
2
votes
0answers
76 views

Fetching photos and tweets related to selected publications

1) Postgresql 2) User has_many :reports 3) Report belongs_to :user 3) Report belongs_to :publication 4) Publication has_many :reports 4) Publication scope :photos, -> ...
2
votes
1answer
118 views

Listing cities in an itinerary

I have a has_many through relationship between my cities and travels that looks like this: ...
3
votes
0answers
116 views

Extracting dynamic behaviors of state machines to modules

This is my initial class. (I am not trying to reinvent state-flows again, just practicing) As you can see straight forward, it dynamically generates methods like .recommend,.recommend!, ....
5
votes
1answer
311 views

Rails models for Users, Offers, Comments, Documents, and Reviews

I want to simplify my Rails models, current looks like this: ...