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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:34:45+00:00 2026-05-18T00:34:45+00:00

I am building a model which keeps track of posts that a given user

  • 0

I am building a model which keeps track of posts that a given user has read (in Rails 3). The table that tracks this is ‘readings.’ a reading belongs_to both :user and :post, both of which has_many :readings. When a user sees a post, it is marked as read, and a ‘reading’ is created with the user_id and post_id. This seems to work fine, but now I am trying to write a scope that returns only unread posts for a given user. Finding read posts works fine:

scope :read, lambda { |user_id = nil |       
    user_id.nil? ? {} : joins(:readings).where('readings.user_id = :user_id', :user_id => user_id)
}

However my attempts to return the complement of this set (unread posts) are failing. How can this be done? Is there a general way to write a scope(method) that just returns the complement of another(here, :read)? I would also be happy to write this as a class method that works the same as a scope.

Bear in mind that readings are only created when a post is read, so a new post would have no readings. This seemed more economical than creating entries for every user for every new post.

EDIT: OK, I am getting closer – the below scope almost works, but does not require that all readings for a post are not equal to user_id. Thus if user #1 has read a post, and so has user #2, that other reading is queried and found to be not equal. So the post is wrongly found to be unread.

scope :unread, lambda { |user_id = nil |       
    user_id.nil? ? {} : includes(:readings).where('(readings.user_id IS NULL OR readings.user_id != :user_id)', :user_id => user_id)
}
  • 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-18T00:34:46+00:00Added an answer on May 18, 2026 at 12:34 am

    Well, it looks like the below works. Still testing though. If anyone finds a hole in here happy to re-open.

    scope :unread_by, lambda { |user_id = nil, hide_unread = nil|
          { :joins => "LEFT JOIN readings ON  readings.post_id    = post.id
              AND readings.user_id        = #{user_id}",
              :conditions => 'readings.id IS NULL' }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an app in Rails 3. I currently have a user model which
I'm building a gui component that has a tree-based data model (e.g. folder structure
I am building an MVC 3 application which uses the standard Model validation attributes
I'm building a model to list the students that have a birthday during the
I am building an application that will have a model that belongs_to another model
I'm looking at building a Rails application which will have some pretty large tables
I'm building a web application with Ruby on Rails which needs to be highly
I'm currently working on redesigning an application which is building a model from input
I'm building a really crude GUI to model mapper which basically traverses all TEdit
When building a transactional system that has a highly normalized DB, running reporting style

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.