Skip to content

Instantly share code, notes, and snippets.

@lazaronixon
Last active April 24, 2025 19:16
Show Gist options
  • Save lazaronixon/142c6e7efbdf97c01bf6cc0b9205a91c to your computer and use it in GitHub Desktop.
Save lazaronixon/142c6e7efbdf97c01bf6cc0b9205a91c to your computer and use it in GitHub Desktop.
has_one_latest_by
class Member
has_one :latest_activity, -> { from(Activity.latest_by_member, :activities) }, class_name: "Activity"
end
class Activity
scope :latest_by_member, -> { select("DISTINCT ON (member_id) *").order(:member_id, created_at: :desc) }
end
@lazaronixon
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment