Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
U

unread

> 后端框架
Open source

Handle unread records and mark them as read with Ruby on Rails

741 stars0 likes4 views
WebsiteGitHub

About

Handle unread records and mark them as read with Ruby on Rails

Unread

Ruby gem to manage read/unread status of ActiveRecord objects - and it's fast.

Features

  • Manages unread records for anything you want readers (e.g. users) to read (like messages, documents, comments etc.)
  • Supports mark as read to mark a single record as read
  • Supports mark all as read to mark all records as read in a single step
  • Gives you a scope to get the unread records for a given reader
  • Needs only one additional database table
  • Most important: Great performance

Requirements

  • Ruby 3.1 or newer
  • Rails 6.1 or newer (including Rails 7.2)
  • MySQL, PostgreSQL or SQLite
  • Needs a timestamp field in your models (like created_at or updated_at) with a database index on it

Changelog

https://github.com/ledermann/unread/releases

Installation

Step 1: Add this to your Gemfile:

ruby
gem 'unread'

and run

bash
bundle

Step 2: Generate and run the migration:

bash
rails g unread:migration
rake db:migrate

Upgrade from previous releases

If you upgrade from an older release of this gem, you should read the upgrade notes.

Usage

…

Getting read/unread stats through a relationship

ruby
class Document  {20=>{:unread=>1, :total=>10}, 82=>{:unread=>0, :total=>4}

Using with_read_marks_for here is the key. It uses just one query and makes sure that the following unread? invocations use the result of the first query.

How does it work?

The main idea of this gem is to manage a list of read items for every reader after a certain timestamp.

The gem defines a scope doing a LEFT JOIN to this list, so your app can get the unread items in a performant manner. Of course, other scopes can be combined.

It will be ensured that the list of read items will not grow up too much:

  • If a user uses "mark all as read", his list gets deleted and the timestamp is set to the current time.
  • If a user never uses "mark all as read", the list will grow and grow with each item he reads. But there is help: Your app can use a cleanup method which removes unnecessary list items.

Overall, this gem can be used for large data. Please have a look at the generated SQL queries, here is an example:

ruby
# Assuming we have a user who has marked all messages as read on 2010-10-20 08:50
current_user = User.find(42)

# Get the unread messages for this user
Message.unread_by(current_user)

Generated query:

sql
SELECT messages.*
FROM messages
LEFT JOIN read_marks ON read_marks.readable_type = "Message"
                    AND read_marks.readable_id = messages.id
                    AND read_marks.reader_id = 42
                    AND read_marks.reader_type = 'User'
                    AND read_marks.timestamp >= messages.created_at
WHERE read_marks.id IS NULL
AND messages.created_at > '2010-10-20 08:50:00'

Hint: You should add a database index on messages.created_at.

Copyright (c) 2010-2023 Georg Ledermann and contributors, released under the MIT license

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecordrailsrubyrubygems

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架