Git Product home page Git Product logo

Comments (7)

brendon avatar brendon commented on May 18, 2024

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.

ledermann avatar ledermann commented on May 18, 2024

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.

brendon avatar brendon commented on May 18, 2024

Thanks @ledermann, yep it's working great. Thanks for the help :)

from unread.

Frexuz avatar Frexuz commented on May 18, 2024

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.

brendon avatar brendon commented on May 18, 2024

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.

Frexuz avatar Frexuz commented on May 18, 2024

Let me give it a go :) Then @ledermann can adjust

from unread.

Frexuz avatar Frexuz commented on May 18, 2024

PR here #98

from unread.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.