Rails, how to sort the array of active records by created_at
You can sort an array by using sort_by.
In your case, you could do the following:
@total.sort_by(&:created_at)
Update:
- Remove @all << @find_user and @all_entry << @find_referal_user
- Why? Because otherwise you will have an array with multiple arrays
- Replace @total with: @total = @find_user + @find_referal_user
- Why? Because @total will now only consists of one merged array with all objects, ready to get sorted through .sort_by(&:created_at).
Rails, how to sort the array of active records by created_at
You can sort an array by using sort_by.
In your case, you could do the following:
@total.sort_by(&:created_at)
Update:
- Remove @all << @find_user and @all_entry << @find_referal_user
- Why? Because otherwise you will have an array with multiple arrays
- Replace @total with: @total = @find_user + @find_referal_user
- Why? Because @total will now only consists of one merged array with all objects, ready to get sorted through .sort_by(&:created_at).
Help with sorting records in Ruby on Rails
You should be able to group your query:
Score.order('score DESC').group('user_id').limit(3)
Sort Records according to associated data column
we can get it simply by using Order with includes
@collection = @collection.includes(:countries).order('countries.name ASC').page(params[:page]).per(per_page)
I don't know when I tried to do this with join I got repeated records and I have to add distinct and after that order did not work.
Re sorting active records in rails without re querying the database?
You are probably looking for the sort_by method. It will allow you to sort a collection on the server side without using any active record query.
This code result :
@products = Product.all
@products = @products.sort_by {|product| product.name} # Ruby Sorting
should be the same as
@products = Product.order(:name) # SQL sorting
Ruby on Rails: How to limit the results of sorting active record results
you can simply add .first(5)
or .last(5).reverse
<%= @top5 = User.all.sort{|a,b| a.questions.where(ques_num: 2).count <=> b.questions.where(ques_num: 2).count}.reverse.first(5) %>
or
<%= @top5 = User.all.sort{|a,b| a.questions.where(ques_num: 2).count <=> b.questions.where(ques_num: 2).count}.last(5).reverse %>
Ruby on Rails: how to sort data in an array of active record objects right before iterating?
@menu_bar.link_pages.sort_by { |e| e.sequence }.each do |lp|
. . .
Ruby on Rails - Order records by created_at and sort_by by boolean
Max's answer is the way to go, the only thing missing in his comment was wrapping it in Arel.sql
posts = Post.active.order(Arel.sql("CASE WHEN pinned THEN 0 ELSE 1 END"), "created_at DESC"))
How to Sort Record in Ruby on Rails based on Last Record Timestamp from References Table
Thanks for everyone has help me to solve this probel. I have my own answer. Here is it.
SELECT chat_rooms.*, (SELECT chat_room_messages.created_at FROM chat_room_messages WHERE chat_room_messages.chat_room_id = chat_rooms.id ORDER BY chat_room_messages.id DESC LIMIT 1) AS last_message_at FROM chat_rooms INNER JOIN chat_room_members ON chat_room_members.chat_room_id = chat_rooms.id WHERE chat_room_members.user_id = #{@current_user.id} ORDER BY last_message_at DESC
I use raw query for solve this problem. Hope this can help anyone who need it !
How do I sort records based on sum of a particular attribute in a set of associations in Ruby on Rails?
You should be able to order by the sum of associated records columns like this
Comment.joins(:ratings).group('comments.id').order('sum(ratings.score) desc')
Related Topics
Retrieving SQL Queries from Active-Record Queries in Rails 3
Simple, Fast SQL Queries for Flat Files
If Exists Statement in SQL to Linq
Maximum Length of an SQL Query
Add Indexes to Speed Up Geocoder Near Search
Formatting Clear and Readable SQL Queries
How to Check for The SQL Server Version Using Powershell
Returning The First X Records in a Postgresql Query with a Unique Field
How to Convert a SQL Subquery to a Join
How to Order by Column with Non-Null Values First in Sql
Iterate Through a List of Strings in SQL Server
Rails Order by Association Field
Rails - Find with Condition in Rails 4
Joining Two Separate Queries in a Postgresql ...Query... (Possible or Not Possible)
Rails Activerecord Query Using Inner Join
How to Select The Record with The 2Nd Highest Salary in Database Oracle