GlobalID uniquely identifies a model instance. There is support for GlobalID in Active Record that allows us to pass the active record objects, that are currently live, instead of class/id pairs, that we have to manually deserialize.
Let’s take a look at it with the help of an example.
This is an example before using GlobalID:
class studentDelete < ApplicationJob
def perform(student_class, student_id, temp)
student = student_class.constantize.find(student_id)
trashable.cleanup(temp)
end
end
After using GlobalID
, it will look like:
class studentDelete < ApplicationJob
def perform(student, temp)
student.cleanup(temp)
end
end
By using GlobalID we can easily locate the active record model without having to conditionally check which active record model we should be querying with. GlobalID makes the code more concise (as illustrated above).
Free Resources