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

  • Home
  • SEARCH
  • 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 8006853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:38:50+00:00 2026-06-04T17:38:50+00:00

I have a notifications table in my database which contains the attributes: timestamp:datetime and

  • 0

I have a notifications table in my database which contains the attributes: timestamp:datetime and read:boolean. What I want is to query and order my notifications; first by if they are read or not, then after which timestamp they’ve got, then limit the amount of notifications to 10.

The query I’ve tried looks something like this:

@user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10)

Which only gives me an error which I narrowed down to proving the boolean field as the culprit.

Is there an existing way of ordering by true/false values or should I resort to using some kind of Custom Field Serialization transforming True’s and False’s to 1’s and 0’s?

  • 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-04T17:38:53+00:00Added an answer on June 4, 2026 at 5:38 pm

    What you specify works for me with Mongoid 2.4.10, mongo 1.3, rails 3.2.3. Hope that the following helps to address your problem.

    class User
      include Mongoid::Document
    
      field :name, type: String
      has_many :notifications
    end
    
    class Notification
      include Mongoid::Document
    
      field :read, type: Boolean
      field :timestamp, type: DateTime
      belongs_to :user
    end
    

    test/unit/notification_test.rb

    require 'test_helper'
    
    class NotificationTest < ActiveSupport::TestCase
      def setup
        User.delete_all
        Notification.delete_all
      end
    
      test "order_by boolean" do
        @user = User.create(name: 'Gary')
        [
          [true, 1.day.ago], [false, 2.days.ago], [false, 3.days.ago], [true, 5.days.ago], [false, 8.days.ago], [true, 11.days.ago],
          [false, 4.days.ago], [true, 6.days.ago], [true, 7.days.ago], [false, 9.days.ago], [false, 10.days.ago]
        ].each do |read, timestamp|
          @user.notifications << Notification.create(read: read, timestamp: timestamp)
        end
        assert_equal(1, User.count)
        assert_equal(11, Notification.count)
        result = @user.notifications.order_by([[:read,:desc],[:timestamp,:desc]]).limit(10).to_a
        assert_equal(10, result.size)
        result.each do |r|
          p [r.read, r.timestamp]
        end
      end
    end
    

    test output

    Run options: --name=test_order_by_boolean
    
    # Running tests:
    
    [true, Mon, 28 May 2012 12:33:49 -0400]
    [true, Thu, 24 May 2012 12:33:49 -0400]
    [true, Wed, 23 May 2012 12:33:49 -0400]
    [true, Tue, 22 May 2012 12:33:49 -0400]
    [true, Fri, 18 May 2012 12:33:49 -0400]
    [false, Sun, 27 May 2012 12:33:49 -0400]
    [false, Sat, 26 May 2012 12:33:49 -0400]
    [false, Fri, 25 May 2012 12:33:49 -0400]
    [false, Mon, 21 May 2012 12:33:49 -0400]
    [false, Sun, 20 May 2012 12:33:49 -0400]
    .
    
    Finished tests in 0.023062s, 43.3614 tests/s, 130.0841 assertions/s.
    
    1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in database which contains start date and end date. Now
I have a model called Notifications, which basically acts as a join table between
Given a Database that has a table Called [Bid Transactions], which contains records of
I have the following database table below I want to display notification to the
I have code like: @notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>deliver_by >= '#{Date.today.to_s(:db)}') logger.info @upcoming_reminders[0].inspect
I have a web app (.NET 3.5) which is sending notifications by email to
I have created an activity which sends a number of notifications to status bar.
I have a Java servlet from which I need to send few notifications to
Possible Duplicate: monitoring mysql for changes I have a MySQL database and i want
I have a table which has event-details. New records will be inserted to this

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.