Comments (3)
with_read_marks_for
is exactly for this use case: Getting a list of items including their read status with a minimal amount of database queries. Example:
In the controller:
@messages = Message.with_read_marks_for(current_user)
In the view:
- @messages.each do |message|
- if message.unread?(current_user)
= content_tag :strong, message.subject
- else
= message.subject
Hope this helps.
from unread.
I don't think it behave right. I am using PostgreSql.
irb(main):004:0> Ticket.with_read_marks_for(u).first.unread?(u)
Ticket Load (1.0ms) SELECT tickets., read_marks.id AS read_mark_id FROM "tickets" LEFT JOIN read_marks ON read_marks.readable_type = 'Ticket'
AND read_marks.readable_id = tickets.id
AND read_marks.user_id = 1
AND read_marks.timestamp >= tickets.updated_at WHERE "tickets"."is_destroyed" = 'f' ORDER BY "tickets"."id" ASC LIMIT 1
=> false
irb(main):005:0> Ticket.with_read_marks_for(u).last.unread?(u)
Ticket Load (1.1ms) SELECT tickets., read_marks.id AS read_mark_id FROM "tickets" LEFT JOIN read_marks ON read_marks.readable_type = 'Ticket'
AND read_marks.readable_id = tickets.id
AND read_marks.user_id = 1
AND read_marks.timestamp >= tickets.updated_at WHERE "tickets"."is_destroyed" = 'f' ORDER BY "tickets"."id" DESC LIMIT 1
ReadMark Load (1.4ms) SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."user_id" = $1 AND "read_marks"."readable_type" = 'Ticket' AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT 1 [["user_id", 1]]
=> true
In the case it is ture, it queries the database twice. One for the list of readables and one for the read_marks.
I have a pull request. Please ignore it if I am wrong. Thanks.
from unread.
Yes, the database has to be touched twice in some cases. Please have a look at the code:
https://github.com/ledermann/unread/blob/master/lib/unread/readable.rb#L102
It's required to get the global timestamp if the message has no read marker.
Here is another example. You will see that the executed queries are optimized:
n = 10
Message.with_read_marks_for(user).last(n).map { |message| message.unread?(user) }
For this, only 2 queries are needed, not n
or 2*n
queries.
Your PR #20 looks interesting, though. Maybe the 2 queries can be combined into a single one. I will check it.
from unread.
Related Issues (20)
- Utilizing current time instead of matching reference timestamp field HOT 6
- operator does not exist: integer = uuid HOT 4
- Create a mark_as_read method (without !)
- seeing n+1 queries even if using with_read_marks_for HOT 2
- Full Table Scan on Insert Causes Deadlock
- Migration contain `null` value for timestamps
- issue with mark_as_read! method HOT 1
- ReadMark.readable_classes is nil until the first use
- Adding a new .read?(current_user) instance method in readable.rb InstanceMethods
- UnreadMigration timestamp column without precision option for rails 6 HOT 1
- Only get NULL timestamp HOT 2
- mark_as_read! :all, for: current_user is not working as intended HOT 1
- mark_as_read! fails with exception on Rails 6+ if Reader has default_scope defined
- problems with uuid indexes HOT 1
- Project dead? HOT 4
- Performance question with indexes HOT 1
- Argument Error on User's Save
- `cleanup_read_marks` killing database CPU for hours? HOT 1
- New users has every notification as read.
- mark_as_read! :all for associations HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unread.