{"id":13878686,"url":"https://github.com/jcypret/hashid-rails","last_synced_at":"2025-10-08T17:17:46.187Z","repository":{"id":733442,"uuid":"33831390","full_name":"jcypret/hashid-rails","owner":"jcypret","description":"Use Hashids (http://hashids.org/ruby/) in your Rails app ActiveRecord models.","archived":false,"fork":false,"pushed_at":"2023-07-31T13:28:39.000Z","size":142,"stargazers_count":356,"open_issues_count":15,"forks_count":47,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-09T11:05:00.360Z","etag":null,"topics":["hashids","rails","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jcypret.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-04-12T19:53:40.000Z","updated_at":"2025-04-27T02:50:37.000Z","dependencies_parsed_at":"2024-01-13T20:37:47.750Z","dependency_job_id":"9e97aa06-3666-4776-b0fc-10501ef0411b","html_url":"https://github.com/jcypret/hashid-rails","commit_stats":{"total_commits":130,"total_committers":10,"mean_commits":13.0,"dds":"0.11538461538461542","last_synced_commit":"e0e2982583fc32a635d1fd5da937588fb04a8e03"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcypret%2Fhashid-rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcypret%2Fhashid-rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcypret%2Fhashid-rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jcypret%2Fhashid-rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jcypret","download_url":"https://codeload.github.com/jcypret/hashid-rails/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384989,"owners_count":22062422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hashids","rails","ruby"],"created_at":"2024-08-06T08:01:56.774Z","updated_at":"2025-10-08T17:17:41.147Z","avatar_url":"https://github.com/jcypret.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Hashid Rails\n[![Gem Version](https://badge.fury.io/rb/hashid-rails.svg)](https://badge.fury.io/rb/hashid-rails)\n![Build Status](https://github.com/jcypret/hashid-rails/workflows/build/badge.svg?branch=master)\n[![Code Climate](https://codeclimate.com/github/jcypret/hashid-rails/badges/gpa.svg)](https://codeclimate.com/github/jcypret/hashid-rails)\n[![Test Coverage](https://codeclimate.com/github/jcypret/hashid-rails/badges/coverage.svg)](https://codeclimate.com/github/jcypret/hashid-rails/coverage)\n\nThis gem allows you to easily use [Hashids](http://hashids.org/ruby/) in your\nRails app. Instead of your models using sequential numbers like 1, 2, 3, they\nwill instead have unique short hashes like \"yLA6m0oM\", \"5bAyD0LO\", and\n\"wz3MZ49l\". The database will still use integers under the hood, so this gem can\nbe added or removed at any time.\n\n\u003e IMPORTANT: If you need to maintain the same hashids from a pre-1.0 release,\n\u003e read the [upgrade notes](#upgrading-from-pre-10).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"hashid-rails\", \"~\u003e 1.0\"\n```\n\nAnd then execute:\n\n```shell\n$ bundle\n```\n\n## Basic Usage\n\n1. Include Hashid Rails in the ActiveRecord model you'd like to enable hashids.\n\n```ruby\nclass Model \u003c ActiveRecord::Base\n  include Hashid::Rails\nend\n```\n\n2. Continue using `Model#find` passing in either a hashid or regular 'ol id.\n\n```ruby\n@person = Person.find(params[:hashid])\n```\n\n## Get hashid of model\n\nYou can access the hashid of any model using the `hashid` method.\n\n```ruby\nmodel = Model.find(params[:hashid])\n#=\u003e \u003cModel\u003e\nmodel.hashid\n#=\u003e \"yLA6m0oM\"\n```\n\nAdditionally, the `to_param` method is overridden to use hashid instead of id.\nThis means methods that take advantage of implicit ID will automatically work\nwith hashids.\n\n```erb\nPassing a hashid model to `link_to`…\n\u003c%= link_to \"Model\", model %\u003e\n\nwill use `hashid` instead of `id`.\n\u003ca href=\"/models/yLA6m0oM\"\u003eModel\u003c/a\u003e\n```\n\n## Alternative Usage\n\nYou can use the `Model#find_by_hashid` method to find a record without falling\nback on the standard `find` method.\n\n\n```ruby\n# When a record is found, it returns the record.\n@person = Person.find_by_hashid(params[:hashid])\n#=\u003e \u003cPerson\u003e\n\n# When no record, it returns nil\n@person = Person.find_by_hashid(params[:hashid])\n#=\u003e nil\n\n# A bang (!) version is also available and raises an exception when not found.\n@person = Person.find_by_hashid!(params[:hashid])\n#=\u003e ActiveRecord::RecordNotFound\n```\n\n## Configuration (optional)\n\nYou can add an initializer for Hashid::Rails to customize the options passed to\nthe Hashids gem. This is completely optional. The configuration below shows the\ndefault options.\n\n```ruby\nHashid::Rails.configure do |config|\n  # The salt to use for generating hashid. Prepended with pepper (table name).\n  config.salt = \"\"\n  config.pepper = table_name\n\n  # The minimum length of generated hashids\n  config.min_hash_length = 6\n\n  # The alphabet to use for generating hashids\n  config.alphabet = \"abcdefghijklmnopqrstuvwxyz\" \\\n                    \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\" \\\n                    \"1234567890\"\n\n  # Whether to override the `find` method\n  config.override_find = true\n\n  # Whether to override the `to_param` method\n  config.override_to_param = true\n\n  # Whether to sign hashids to prevent conflicts with regular IDs (see https://github.com/jcypret/hashid-rails/issues/30)\n  config.sign_hashids = true\nend\n```\n\n### Model-Level Config\n\nYou can also customize the hashid configuration at the model level.\n`hashid_config` supports all the same options as the `Hashid::Rails.configure`\nblock and allows for each model to have a different config. This can be useful\nfor setting a custom salt/pepper. For instance, the pepper defaults to the table\nname, so if you rename the table, you can keep the same hashids by setting the\npepper to the old table name.\n\n```ruby\nclass Model \u003c ActiveRecord::Base\n  include Hashid::Rails\n  hashid_config pepper: \"old_table_name\"\nend\n```\n\n## Upgrading from Pre-1.0\n\nThe 1.0 release of this gem introduced hashid signing to prevent\nconflicts with database IDs that could be mis-interpreted as hashids.\nIDs are now signed when encoding and the signature verified when decoding.\nThe trade off is that hashids are different than in previous versions due to the added signature.\nIf you need to maintain the same hashids from a pre-1.0 version, set `sign_hashids` to false in the config.\n\nAdditionally, some of the config names have been modified to better match the parent [Hashid](https://github.com/peterhellberg/hashids.rb) project.\nThe config `secret` has been renamed to `salt` and the `length` renamed to `min_hash_length`.\nUpdate the initializer config accordingly.\n\nLastly, `Hashid::Rails` is no longer imported into `ActiveRecord::Base` by default.\nYou can instead include `Hashid::Rails` selectively in the desired models,\nor include it in `ApplicationRecord` for Rails 5 to apply to all subclassed models,\nor add an initializer with `ActiveRecord::Base.send :include, Hashid::Rails` to match previous behavior.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run\n`bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To\nrelease a new version, update the version number in `version.rb`, and then run\n`bundle exec rake release` to create a git tag for the version, push git commits\nand tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\n1. Fork it ( https://github.com/[my-github-username]/hashid-rails/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n\u003e NOTE: If it's a significant feature or change, consider creating an Issue for\n\u003e discussion before opening a PR.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcypret%2Fhashid-rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcypret%2Fhashid-rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcypret%2Fhashid-rails/lists"}