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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:00:36+00:00 2026-05-27T11:00:36+00:00

The sql statement is like this: select posts.id, posts.title from posts inner join (select

  • 0

The sql statement is like 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; 

I want to get a rails 3 sql statement equivalent to the above one.

What i tried is given below:

The following sql query gives similar result.

select distinct post_id, created_at  
from comments 
order by created_at DESC limit 5

@temp = Comment.select('distinct(comments.post_id),created_at').order('created_at DESC').limit(5)

I tried to join this derived @temp table with posts table to get the posts.title

I tried this but failed.

@temp2 = @temp.joins('posts')

So, How can i join posts table with derived @temp 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 "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)

posts model, has_many :comments, comments model, belongs_to :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-05-27T11:00:37+00:00Added an answer on May 27, 2026 at 11:00 am

    Question author needs to read up on basic Rails and activerecord usage before jumping into SQL. Need to understand how Activerecord models your data and how to use it. First figure out what you want to do in common language and then see how you can use what exists to do it.

    Rails does not know the structure of your @temp table. It only has a result set and from what I understand, AREL does not build logic from the result set. It builds from schemas which it pulls for active record models.

    You cannot build a view from this data, so your only option is to use the standard join options with activerecord classes or to do custom sql.

    In Rails 3, the ActiveRecord relational algebra is very advanced and has made queries very easy.

    Comment.order("#{Comment.table_name}.created_at desc').limit(5).joins(:posts).order("#{Post.table_name} created_at desc")

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

Sidebar

Related Questions

i have sql statement like this SELECT DISTINCT results_sp_08.material_number FROM results_sp_08 INNER JOIN courses
I have a sql statement like this: select a.id, a.valfrom ... inner join ...
I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0;
If i have a parameterized SQL statement like this: SELECT * FROM table WHERE
I have a SQL select statement like this: select FirstName, LastName, Age from People
When I write an SQL statement, I usually write it like this SELECT COUNT(*)
I want to write an SQL statement like below: select * from tbl where
I have a SQL statement that looks like: SELECT [Phone] FROM [Table] WHERE (
How can I build a query statement like this: select * from men where
I need to write a sql statement like this: SELECT id_segmento AS Segmento, Decode

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.