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
Tag: ruby-on-rails
Total value of warehouse active record
I have the object Tool and I need to calculate the cost of all the Tools in the warehouse. I can sum the column price Tool.sum(:price) but i need to sum (quantity * price) of all the table Tools. Thanks in advance. Answer Check out the example listed for ActiveRecord::Calculations#calculate. You can do the following:
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 force Rails ActiveRecord to use CamelCase in queries, instead of snake_case
I am trying to create a rails app that will connect to a production SQLServer that is used by 3rd party software, so migrating the database is not an option. I am able to connect to the SQL server without any issues (I used this guide for connecting to existing database, and this one to connect rails to SQL Server),
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,
Get the latest child messages and also parent messages that are childless
The following is the Message model message_participants has a record for each message and the various people who have sent or received that message. It has a user_id on it. The problem with the above latest_messages_by_participant scope is that it is able to get all the child messages BUT it gets only the last parent message. That is because we
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 Foreign Key Constraints in Database vs Model
I have the following models in a Rails app. This setup has worked fine since normally when I would like to add a new VendorPromo to a vendor, there’s a dropdown in the view of available Promos to choose from. Now, I’m enabling the creation of new VendorPromos via an API. However, this current set up will allow the creation
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