I would like to write a query to compare created_at timestamp with my current date in Ruby on Rails using ActiveRecord and I don’t quite know how do it yet Date is stored like this -> created_at: …
Tag: activerecord
Get relation of a collection in rails
So simple but can’t find how to do it in rails. I have a specific active record collection of users. Something like users = User.where(blabla). Given this collection is there a simple way to get all …
Ordering records by an association presence
Lets say I have an instructions table which is associated to a surveys table through survey_instructions join table. What I need to achieve is to fetch all instruction records, but ordered by an …
Rails/PostgreSQL – Query to update all dates adding 1 day
I want to execute an update_all query to update a datetime column adding 1 day to the current value of this column on all rows. I could achieve that using .each and .update, but it would be more …
Raills: Get SQL generated by delete_all
I’m not particularly familiar with Ruby on Rails, but I’m troubleshooting an issue we’re experiencing with a rake job that is supposed to be cleaning database tables. The tables grow very large very …
How to use ANY instead of IN in a WHERE clause?
I used to have a query like in Rails: Which generates sql query like: Now I want to change this to use ANY instead of IN. I created this: Now when I use empty array ids = [] I get the folowing error: Answer There are two variants of IN expressions: expression IN (subquery) expression IN (value [, …]) Similarly,
Rails select() and count() don’t seem to play nice
I’ve noticed something odd with Rails (4.1) ActiveRecord, where select and count sometimes mix badly: User.all.count => 103 User.all.size => 103 User.all.length => 103 So far, so good. I …
Rails User.joins.not(…) in Active Record?
Im looking to query all Users without Comments in a single sql query? Models: class User < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :user ...
Rails ActiveRecord – how to fetch records between two dates
I have two date columns – from_date and to_date in a database table. Example: from_date: 2012-09-10 to_date: 2012-09-30 today: 2012-09-13 I need to fetch all records, if today’s date is between from_date and to_date. How do I do that with a SQL query? If I have loaded the respective record, I can easily decide, if today’s date is between from_date
rails scope and joins
I have tried everything i thought would work for this and am turning up nothing. in rails 3, I need to find all users with a cd player in their car. A car has one user and one radio, and a user belongs to a car, and a radio has many cars. I am stumbling on how I would perform