Git Product home page Git Product logo

Comments (3)

ledermann avatar ledermann commented on May 18, 2024

with_read_marks_foris 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.

Ian-Choi avatar Ian-Choi commented on May 18, 2024

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.

ledermann avatar ledermann commented on May 18, 2024

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 nor 2*nqueries.

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)

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.