fizzy
fizzy copied to clipboard
Refactor: use idiomatic .last instead of .order(:desc).first
Simplifies the last_event method in ActivitySpike::Detector by using the more idiomatic Rails pattern .order(:created_at).last instead of .order(created_at: :desc).first. Both generate the same SQL query but .last is more readable and conventional in Rails codebases.
fizzy(dev):002> Card.last.events.order(:created_at).last
CACHE Card Load (0.1ms) SELECT "cards".* FROM "cards" ORDER BY "cards"."id" DESC LIMIT ? [["LIMIT", 1]]
Event Load (1.0ms) SELECT "events".* FROM "events" WHERE "events"."eventable_id" = ? AND "events"."eventable_type" = ? ORDER BY "events"."created_at" DESC LIMIT ? [["eventable_id", "<16 bytes of binary data>"], ["eventable_type", "Card"], ["LIMIT", 1]]
fizzy(dev):001> Card.last.events.order(created_at: :desc).first
Card Load (0.3ms) SELECT "cards".* FROM "cards" ORDER BY "cards"."id" DESC LIMIT ? [["LIMIT", 1]]
Event Load (0.8ms) SELECT "events".* FROM "events" WHERE "events"."eventable_id" = ? AND "events"."eventable_type" = ? ORDER BY "events"."created_at" DESC LIMIT ? [["eventable_id", "<16 bytes of binary data>"], ["eventable_type", "Card"], ["LIMIT", 1]]