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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:42:08+00:00 2026-06-15T17:42:08+00:00

I am using Ruby on Rails 3.2.9 and MySQL. I have an Article model

  • 0

I am using Ruby on Rails 3.2.9 and MySQL. I have an Article model with a user_id attribute (this attribute represents the foreign key – id – for associated author users) and I would like to retrieve articles ordering those “for” a given author. That is, given I have following records:

<#Article id: 1, title: "Title 1", user_id: 1>
<#Article id: 2, title: "Title 2", user_id: 2>
<#Article id: 3, title: "Title 3", user_id: 1>
<#Article id: 4, title: "Title 4", user_id: 3>
<#Article id: 5, title: "Title 5", user_id: 1>
...
<#Article id: N, title: "Title N", user_id: M>

When I look for articles ordered “for” the author with id 1 (user_id = 1) then the returning articles should be ordered as-like the following:

<#Article id: 1, title: "Title 1", user_id: 1>
<#Article id: 3, title: "Title 3", user_id: 1>
<#Article id: 5, title: "Title 5", user_id: 1>
<#Article id: 2, title: "Title 2", user_id: 2>
<#Article id: 4, title: "Title 4", user_id: 3>
...
<#Article id: N, title: "Title N", user_id: M>

In other words, I am looking to retrieve all articles but making those ordered with this “priority”: articles created by the given author returned first and then all other articles (that is, I would like to “push ahead” articles created by a given author).

How can I make that?


Note: I am looking for a Ruby on Rails implementation, maybe through the order method.

  • 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-15T17:42:09+00:00Added an answer on June 15, 2026 at 5:42 pm

    Your example is not that clear since you are searching for user_id 1 and a normal ordering by user_id would put those first anyway. I believe you mean to do something like:

    SELECT id, title, user_id
    FROM myTable
    ORDER BY CASE WHEN user_id = @search_id THEN 1 ELSE 2 END, user_id
    

    In your example above, @search_id should be 1.

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

Sidebar

Related Questions

I am using Ruby on Rails 3.2.2 and MySQL. I have a method (an
I have a deployment using: rails 2.3.2 ruby 1.8.7 mysql db and 3 mongrel
I am using Mysql 5 and Ruby On Rails 2.3.1. I have some validations
I have Ruby on Rails application which is using MySQL database. For now I'm
I am using ruby on rails with a MySQL backend. I have a table
I am using Ruby on Rails 3.2.2 and MySQL. From the logger I know
I am reading (mysql) data to display on the view using ruby on rails.
I am using Ruby on Rails, but I think this question could be applied
I am attempting to remotely connect to a MySQL database using ruby on rails.
I have two application servers running Ruby on Rails sharing the same MySQL database

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.