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
Rails: Finding records where a nested association is empty
In a Rails application I’m working on, I’ve got a few different models associated like this (condensed for clarity): group.rb group_member.rb newsletter.rb newsletter/author.rb newsletter/story.rb Given the above associated models, here’s the framework I’m working within: Each Newsletter has n Authors (Group Members) and n Newsletters. Each Group Member can author multiple stories for a given newsletter. Each story is one
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 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 ActiveRecord – Order query by matching number
I have a model called Location. This table has 2 fields I want to order by: rating (integer) and name (string). I want to order alphabetically by name, but if the rating is 5, I want that rating to come first. If the rating is anything else except for 5, I want them to unbiasedly show up in alphabetical order
How to make generic SQL heredocs?
I’m making a mock ORM in Ruby, storing ‘forum’ data. I have the following tables: users (id, fname, lname), and questions (id, title, body, author_id). a_user.create and a_question.create are more or less the same: I’m writing another class, ModelBase, and I’d like to be able to say something like or . Users and Questions do not have an id until
How Select record with where clause with math using ActiveRecord?
I want to return a list of records whose created_at more than 5 seconds before its updated_at i.e. But this syntax doesn’t appear to work. Getting the error: Answer You didn’t say what database you are using, but with MySQL you can the TIMEDIFF function for this: If you are using Postgres:
Why am I able to call the class method as if it were an instance method here?
I was looking through this example: class SQLObject def self.columns return @columns if @columns columns = DBConnection.execute2(<<-SQL).first SELECT "#{table_name}".* ...
Populate an HTML DropDown List with data from mysql database without PHP
I’m currently building a rails app where im trying to fetch data from different tables from my sql database and I need to put that fetched data (id, first_name, last_name) into an HTML Drop-down where …