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: activerecord
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),
How to use a calculated value in Oracle SQL count
I have a column called BIRTH_DATE in my view. I want to calculate the age using that column and today’s date, and count the number of staff in an age bracket. For example: staff between 40 and 49 etc. Other answers in plain SQL statements are also welcome. My code is shown below: I am calculating the age as shown
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 generate an AR query that returns record A, excluding any that are attached to record B of a certain type
My relevant DB tables are an Invoices table and an Advances table with a has_many relationship (an invoice has many advances). I am trying to generate an ActiveRecord query that will return all …
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
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: