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

The Archive Base Latest Questions

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

I have a Post model and a View model (which logs the view of

  • 0

I have a Post model and a View model (which logs the view of each post by a user) as follows:

class View < ActiveRecord::Base
    belongs_to :user, :class_name => 'User'
    belongs_to :post, :class_name => 'Post'
    validates_uniqueness_of :user_id, :scope =>:post_id
end

Each post has a boolean field named “special”.

I want to fetch all posts with following sort conditions:

  1. If a post is ‘special’ AND NOT VIEWED by the current user (say user_id=2), then that should appear on top in the result, internally sorted by id in descending order).
  2. Sort all other posts (not special and viewed-special) on the basis of id field for the posts.

Cant figure out if and how I can do that.

To start with, I tried the following query:

posts = Post.order("(case when special = true then 'special desc, id desc' else 'id desc' end)")

But it behaves in a funny manner: returns all special posts in top followed by regular posts but with ids sorted in ascending order.

What am i missing here? Also, how can I use join (for views table) with such a complex order by clause? I tried to write a SQL for this but struggling there also! Show me some light 🙂

P.S I’m new to Rails and still learning.

  • 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-08T10:09:33+00:00Added an answer on June 8, 2026 at 10:09 am

    The code you posted is, for each row computing one of 2 string literals and ordering based on that which is why you don’t get the examples you want.
    I’d do something like

    Post.order('special desc, id desc')
    

    Since rails stores boolean fields as 0/1 in MySQL this will sort special posts ahead of non special posts.

    Adding views to this doesn’t change a whole lot: you need to left join the views table and do something like

    Post.joins('left join views on posts.id=post_id and user_id=2').order(' ( views.id is null && special)  desc, id desc')
    

    The is null detects whether a view was found which is then and-ed with the post’s specialness.

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

Sidebar

Related Questions

I have a typical, Post model: class Post< ActiveRecord::Base validates_presence_of :user_id #Line 1 validates_presence_of
I have a view which displays a model eg: public class MyModel() { public
I have a view which queries a model to populate a form: class AddServerForm(forms.Form):
Let's say I have User model and Post model. Post model contains field user_id
I have a Category and a Post model, with each Post belonging to a
I have a User model, a Post model, and an Interest model. User has_many
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):
I have View Model with inner ViewModel Address propery. public class CommonViewModel{ public AddressViewModel
I have a Add view which uploads file which is element of Question class
I have a complex view model which contains a collection of other objects which

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.