i am trying to get all records within a date range in rails that fall after a given start date and before a given end date, ignoring the year. the start date will just be a month. the end of the date range is a month and date. the following example is to get users with a hire date month
Tag: ruby-on-rails
Append additional conditions to a query
I have a query where I need to add several conditions as an OR clause, because I want to use LIKE, so I can not use IN instead. So how can I add multiple OR from an array? The SQL should look like this: where ‘a’, ‘b’, ‘c’ is from an array. Answer From the Rails API docs, the .or
Ruby on Rails converting Timescale SQL rate code to Ruby
I’m trying to use the Rate function from timescale to generate graph data. Right now I have a database view that does this using the concepts from the SQL code below from TimescaleDocs: Is there a way to convert this directly to ruby code in a timeseries model to improve runtime? Answer It’s possible, but I bet building the same
How can I make these Active Record queries faster?
I’m using Active Record to send a few queries to a postgres database. I have a User model, a Business model and a joiner named UserBiz. The queries I mentioned go through the entire UserBiz collection and then filter out the businesses that match user provided categories and searches. These “work” but the problem is when I threw a quarter
Rails – deeply nested joins with ActiveRecord
I occasionally need to do a join in my app that goes across multiple tables, but haven’t quite figured out how to do this for more than 3 tables: Currently I’m ready to go: What I would like to do now is to join every consumer that belongs to an OrderListItem. However, when I try the following: I get the
How do I write a Ruby where clause with dates and array lookup?
I want to do something like the above. I first want to check if the model external id is in the array of appointment_ids, then make sure it is not soft deleted and then check if the record start time is between dates.. Any help much appreciated. Answer You don’t need to use a SQL string to create that query.
How to translate this jsonb SQL query to an Active Record query?
The following data is present in my Color ActiveRecord model: id colored_things 1 [{“thing” : “cup”, “color”: “red”}, {“thing” : “car”, &…
Creating a scope in Ruby on Rails, that returns an instance of an object only if all the associated instances fit a certain condition
I have a table called Carts with a has_many relationship to another table called Subcarts. I want to create a scope that returns all instances of the Cart table where all the associated Subcarts fits …
How to count the sum of Invoices by last year using SQL in Rails?
I need to count all amount of invoices for last year. I do it using helper method but, as far as I know, there is better way using only one query to database with SQL function SUM(). My invoices table:…
Rails: How to sort models by an associated model’s field with condition?
I have three models: This is what I want: Feedback model has a result field. If result is true or false, the feedback is closed. If result is nil, the feedback is unclosed. I want to sort from the unit with the highest number of unclosed feedbacks to the unit with the lowest. It should be at the bottom of