In my Rails model, I initially had a function to handle advisory locks in PostgreSQL. Here's the code, which works as expected:
# Postgresql version
def with_advisory_lock
self.class.connection.execute("SELECT pg_advisory_lock(#{self.id})")
yield