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 6916269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:34:48+00:00 2026-05-27T09:34:48+00:00

Possible Duplicate: Ruby on Rails: How can i join with a derived table? posts

  • 0

Possible Duplicate:
Ruby on Rails: How can i join with a derived table?

posts table

                                   Table "public.posts"
   Column    |          Type          |                     Modifiers                      
-------------+------------------------+----------------------------------------------------
 id          | integer                | not null default nextval('posts_id_seq'::regclass)
 title       | character varying(100) | not null
 content     | character varying(500) | not null
 created_at  | date                   | 
 updated_at  | date                   | 
 tags        | character varying(55)  | not null default '50'::character varying
 category_id | integer                | not null default 1
Indexes:
    "posts_pkey" PRIMARY KEY, btree (id)

comments table

                                   Table "public.comments"
   Column   |          Type          |                       Modifiers                       
------------+------------------------+-------------------------------------------------------
 id         | integer                | not null default nextval('comments_id_seq'::regclass)
 post_id    | integer                | not null
 name       | character varying(255) | not null
 email      | character varying(255) | not null
 content    | character varying(500) | not null
 created_at | date                   | 
 updated_at | date                   | 
Indexes:
    "comments_pkey" PRIMARY KEY, btree (id)

I’m trying to make a equivalent rails 3 sql statement of this.

select posts.id, posts.title 
from posts 
inner join (select distinct post_id,created_at  
            from comments 
            order by created_at DESC limit 5
            ) as foo 
        on posts.id=foo.post_id 
order by foo.created_at DESC; 

It’s to get recent commented-post (limit 5). It’s a complex sql query. Need to select posts.title from posts table joining comments table.

  • 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-05-27T09:34:49+00:00Added an answer on May 27, 2026 at 9:34 am

    The Rails3 way to do a query like that is find_by_sql:

    posts = Post.find_by_sql(%q{
        select posts.id, posts.title 
        from posts 
        inner join (select distinct post_id,created_at  
                    from comments 
                    order by created_at DESC limit 5
                    ) as foo 
                on posts.id=foo.post_id 
        order by foo.created_at DESC
    })
    

    You could use joins if you wanted but, IMHO, that would just obfuscate things:

    Post.select('posts.id, posts.title').
         joins('join (select distinct post_id, created_at from comments order by created_at desc limit 5) as foo on posts.id = foo.post_id').
         order('foo.created_at desc')
    

    Or perhaps something even more complicated like this:

    join_to = Comment.select('distinct post_id, created_at').
                      order('created_at desc').
                      limit(5).
                      to_sql
    
    posts = Post.select('posts.id, posts.title').
                 joins("join (#{join_to}) as foo on posts.id = foo.post_id").
                 order('foo.created_at desc')
    

    Some things are just easier and clearer in SQL than AREL. The find_by_sql method exists for a reason:

    find_by_sql provides you with a simple way of making custom calls to the database and retrieving instantiated objects.

    and this sort of thing is exactly what find_by_sql is for. You’ll have to pull out find_by_sql for a lot of advanced SQL features (such as CTEs) and there’s no reason to be afraid of it or avoid it: ActiveRecord is a tool, not a dogmatic way of life. find_by_sql does have downsides (such as not playing that nicely with scopes) but so does the standard AREL stuff (such as not playing nicely with non-trivial SQL).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can't convert String into integer in ruby/ruby-on-rails I'm running through a tutorial
Possible Duplicate: Rails 3.1 and Ruby 1.9.3p125: ruby-debug19 still crashes with “Symbol not found:
Possible Duplicate: Custom model attribute (column name) title in Ruby on Rails I've been
Possible Duplicate: Rails 3.1 and Ruby 1.9.3p125: ruby-debug19 still crashes with “Symbol not found:
Possible Duplicate: Ruby 1.9.2 and Rails 3 cannot open rails console I have already
Possible Duplicate: What does map(&:name) mean in Ruby? Ruby/Ruby on Rails ampersand colon shortcut
Possible Duplicate: Is it possible to run Ruby on Rails with Ruby 1.9x? Browsing
Possible Duplicate: Ruby on Rails scalability/performance? I'm aware from the twitter shortages that Ruby
Possible Duplicate: Perplexing Ruby/MySQL Error: "invalid packet: sequence number mismatch" My rails app just
Possible Duplicate: Why is rake db:migrate:reset not listed in rake -T? Inside my Rails

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.