Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:09:22+00:00 2026-06-08T03:09:22+00:00

Im using the will_paginate gem and rails 3.2.3. Im trying to display all the

  • 0

Im using the will_paginate gem and rails 3.2.3.
Im trying to display all the posts that have certain tags ordered by the latest post so I use this:

  @posts = Post.find_by_post_type_label(session[:current_post_type],@selected_labels)
             .paginate(:page => params[:page], :order => "comments.created_at DESC").all

but it returns the posts out of order. Each page will be sorted, but the second page will have posts that should have been on the first one.

Example:

Post.find_by_post_type_label(1,[])

returns posts [1,2,3,4,5,6,7,8,9,10] with each ones latest comment being that many months ago. The sql it generates is:

 SELECT `posts`.* FROM `posts` WHERE `post`.`post_type_id` = 1

and

 SELECT `comments`.* FROM `comments` WHERE `comments`.`post_id` IN ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ORDER BY created_at DESC

If I order it, and limit it though, like:

Post.find_by_post_type_label(1,[]).order(comments.created_at DESC).limit(5)

which is how the will_paginate does it, it returns posts 2,3,4,5,7

after generating this:

SELECT DISTINCT `post`.id FROM `posts` LEFT OUTER JOIN
`comments` ON `comments`.`post_id` = `post`.`id` WHERE 
`post`.`post_type_id` = 1 ORDER BY posts.created_at DESC LIMIT 5

and

 SELECT `post`.`id` AS t0_r0, `post`.`user_id` AS t0_r1, `post`.`name` AS 
 t0_r2, `post`.`image_id` AS t0_r3, `post`.`post_type_id` AS t0_r4, 
`post`.`description` AS t0_r5, `post`.`featured` AS t0_r6, `post`.`approved`
AS t0_r7, `comment`.`id` AS t1_r0, `comments`.`post_id` AS t1_r1, 
`comments`.`image_id` AS t1_r2, `comments`.`edits` AS t1_r3, 
`comments`.`color` AS t1_r4, `comments`.`min_reputation` AS t1_r5, 
`comments`.`created_at` AS t1_r6, `comments`.`updated_at` AS t1_r7 FROM 
`post` LEFT OUTER JOIN `comments` ON `comments`.`post_id` = 
`post`.`id` WHERE `post`.`post_type_id` = 1 AND `post`.`id` IN (2, 3, 
4, 5, 7) ORDER BY comments.created_at DESC

It is clearly limiting before it is ordering, but I cant figure out how to change that.
Any help would be appreciated

Edit 1

The relationships are like so:

Class Post
  belongs_to :user
  belongs_to :post_type
  has_many :comments, dependent: :destroy, order: 'created_at DESC'
  has_and_belongs_to_many :labels

Class Comment
  belongs_to :post
  belongs_to :user

Comments are the only ones with timestamps in the database. The first comment holds the created at time for the post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:09:23+00:00Added an answer on June 8, 2026 at 3:09 am

    Winded up using this sql in a find_by_sql

    SELECT  p1.*, iv.created_at FROM posts p1
    INNER JOIN ( SELECT p2.id id, max(c.created_at) created_at FROM posts p2
                INNER JOIN comments c ON c.post_id = p2.id
                GROUP BY p2.id)
    iv ON p1.id=iv.id ORDER BY iv.created_at DESC LIMIT 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an AR query using 'will_paginate' that looks like this: paginate :all, :page
I'm using the will_paginate gem for my Rails project and it's working beautifully. Unfortunately,
I'm using the will paginate gem from http://github.com/mislav/will_paginate Routes: map.resources :post do |post| post.resources
Am using below specified gem acts-as-taggable-on(2.2.2) will_paginate(3.0.2) rails(3.0.3) ruby 1.8.7 i tried to use
I'm using will_paginate to display data returned from a query that includes both a
I have a old rails project that is using rails 3.0.9 and I am
I'm using will_paginate with a HABTM relationship between Blog Posts and Tags. Whenever I
I'm using the will_paginate gem to display a collection in a paginated fashion. Now
I am trying to ajaxify will_paginate result in rails 3.1. I have gone through
I have a list of elements, and I've been using will_paginate up until now,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.