Comments (7)
I ended up not eager loading the comments at all (as I don't actually use the objects). Instead I'm generating a statistics hash like this:
default_hash = Hash.new { |h, k| h[k] = { unread: 0, total: 0 } }
document.comments.with_read_marks_for(current_user).reduce(default_hash) do |hash, comment|
hash[comment.value_id][:unread] += 1 if comment.unread?(current_user)
hash[comment.value_id][:total] += 1
hash
end
Results in one query at the start plus several (ultimately cached) queries on the read_marks
table when there are no read_marks. Works pretty well.
from unread.
Using with_read_marks_for
is the key. It uses just one query and make sure that the following unread?
invocations use the result of the first query (see code).
Your solution looks fine.
from unread.
Thanks @ledermann, yep it's working great. Thanks for the help :)
from unread.
Just stumbled upon this. @brendon's solution was exactly what we were looking for.
It would be a great addition to the README (the whole example?), and explaining that subsequent unread?(user)
doesn't do an additional query. 👍
A bit unnecessary to dig through the issues / finding it by chance :)
from unread.
Glad I was able to be of some help @Frexuz :) I'm sure @ledermann would appreciate a PR with the documentation enhancements if you had the time to do it. Would you be keen? :)
from unread.
Let me give it a go :) Then @ledermann can adjust
from unread.
PR here #98
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.