Add Support for Converting ULID Binary Data to String in Rails #39
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces new functionality to the ULID module, aimed at simplifying the interaction with ULID values stored as binary in MySQL databases when using Rails. Working directly with binary formats in Rails can be cumbersome, especially when dealing with model attributes. To address this, we've added methods to convert ULID values between binary and string representations seamlessly.
Background
Our application utilizes ULIDs as a more sortable and time-precise alternative to UUIDs. These ULIDs are generated and stored in a MySQL database in their binary form to save space and ensure efficient querying. However, Rails' native handling of binary data requires verbose and repetitive conversion code, hindering developer productivity and increasing the risk of errors.
Implementation
To address this issue, we've implemented a set of utility functions within the ULID module, specifically within the Generator submodule. These functions are designed to seamlessly integrate with Rails' ActiveRecord, allowing for the automatic conversion of binary ULID data to strings when records are loaded from the database, and vice versa when data is saved.
Key Additions:
binary_to_ulid_string
: Converts binary ULID data to a string.ulid_string_to_binary
: Converts a ULID string back to binary format.These methods extend the existing ULID module's functionality, making it more compatible with Rails applications and simplifying the handling of ULID identifiers.
Usage Example
To leverage the new functionality, we recommend using a custom ActiveRecord type. This approach encapsulates the conversion logic, ensuring that ULID fields are automatically handled throughout the application.
Here's how to define a custom type for ULID:
With this custom type in place, you can now declare ULID attributes in your models like so:
This setup ensures that ULID attributes are automatically converted to strings when accessed and converted back to binary when saved to the database, simplifying the use of ULID in a Rails context.
Conclusion
By integrating these utility functions into the ULID module and leveraging ActiveRecord's custom types, we can significantly streamline the handling of ULID identifiers in Rails applications. This enhancement not only improves developer efficiency but also maintains the integrity and performance benefits of storing ULIDs in their binary form in the database.