Skip to content
Advertisement

Tag: ruby-on-rails

Where with OR condition for the same column in Rails

I’m trying to retrieve all records that have expired_at as nil and expired_at greater than the current time. I’ve tried this: But that is only returning me the records expired_at greater than DateTime.now. Why is the nil being neglected? Answer Your current query checks that expired_at is equal to null (via = NULL) which will not evaluate to true. Instead,

Find all records which have a count of an association of zero and none-zero

I’d like to get all the images which are being used, and a different query to get all the images that are not being used (based on AssociatedImage). I tried Image.joins(:associated_images).group(‘images.id’).having(‘count(image_id) > 0’) and it returns the correct result. But when I run Image.joins(:associated_images).group(‘images.id’).having(‘count(image_id) = 0’), it returns an empty #<ActiveRecord::Relation []> and I’m not sure why is that. My

How to avoid rails as_json creating flood of database requests

I have a model questions that has many tags through another model question_tags. I am trying to write an api endpoint that returns a list of questions and for each question a list of it’s related tag’s name and id. I’m currently using; But this runs a separate db request for every question in questions is there an alternative that

Ruby on Rails Database Query with Ruby Variable

This is the current code for my query.. I want to be able to add something like But everything I try doesn’t seem to work due to the “property_id” portion. I’ve tried concatenating, different assignments, etc. I feel dumb. SOS. Thank you ahead of time. Also, when I add.. Which “hotelid1” is an existing hotel_id in the table. It works

Advertisement