### Example Prefixed IDs Source: https://github.com/excid3/prefixed_ids/blob/main/README.md These are examples of how prefixed IDs will look for different models. ```ruby user_12345abcd acct_23lksjdg3 ``` -------------------------------- ### Install Prefixed IDs Gem Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Add the gem to your application's Gemfile to start using prefixed IDs. ```ruby gem 'prefixed_ids' ``` -------------------------------- ### Get Prefix ID using `#to_param` or `#prefix_id` Source: https://context7.com/excid3/prefixed_ids/llms.txt When `override_param` is true (default), `to_param` returns the prefix ID for URL generation. Otherwise, use the `#prefix_id` method directly. ```ruby user = User.find(1) # With override_param: true (default) user.to_param #=> "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" user_path(user) #=> "/users/user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" # With override_param: false user.prefix_id #=> "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" ``` -------------------------------- ### Get Prefixed ID for Record Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Use the `to_param` method to get the prefixed ID for a record when `to_param` override is enabled. ```ruby @user.to_param #=> "user_12345abcd" ``` -------------------------------- ### #to_param / #prefix_id — Get the Prefix ID for a Record Source: https://context7.com/excid3/prefixed_ids/llms.txt Retrieves the prefix ID for a record. If `override_param` is true (default), `to_param` returns the prefix ID, which is automatically used by Rails URL helpers. If `override_param` is false, use the `#prefix_id` instance method directly. ```APIDOC ## `#to_param` / `#prefix_id` — Get the Prefix ID for a Record When `override_param: true` (the default), `to_param` returns the prefix ID and Rails URL helpers use it automatically. When the override is disabled, use the `#prefix_id` instance method directly. ```ruby user = User.find(1) # With override_param: true (default) user.to_param #=> "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" user_path(user) #=> "/users/user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" # With override_param: false user.prefix_id #=> "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" ``` ``` -------------------------------- ### Get Prefixed ID Directly Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Access the `prefix_id` attribute directly if the `to_param` override is disabled. ```ruby @user.prefix_id #=> "user_12345abcd" ``` -------------------------------- ### Global Configuration Source: https://context7.com/excid3/prefixed_ids/llms.txt Set global defaults for salt, delimiter, alphabet, and minimum length in an initializer. Per-model options passed to `has_prefix_id` override these. ```APIDOC ## Global Configuration — Salt, Delimiter, Alphabet, Minimum Length ### Description Set global defaults for the PrefixedIds gem. These can be overridden on a per-model basis. ### Configuration Options - **PrefixedIds.salt** (string) - Makes IDs unguessable. Recommended to set from an environment variable. - **PrefixedIds.delimiter** (string) - The delimiter used between the prefix and the ID. Default: `_`. - **PrefixedIds.minimum_length** (integer) - The minimum length of the encoded ID. Default: `24`. - **PrefixedIds.alphabet** (string) - The set of characters used for encoding IDs. ``` -------------------------------- ### Configure Global Prefixed IDs Settings Source: https://context7.com/excid3/prefixed_ids/llms.txt Set global defaults for the Prefixed IDs gem in an initializer file. These settings can be overridden by per-model configurations. ```ruby # config/initializers/prefixed_ids.rb PrefixedIds.salt = ENV["PREFIXED_IDS_SALT"] # makes IDs unguessable PrefixedIds.delimiter = "_" # default: "_" PrefixedIds.minimum_length = 24 # default: 24 PrefixedIds.alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ``` -------------------------------- ### Look Up Record by Prefix ID using `.find` Source: https://context7.com/excid3/prefixed_ids/llms.txt When `override_find` is true (default), `.find` accepts either a prefix ID string or a plain integer primary key. `fallback` option controls if integer IDs are still accepted. ```ruby # Look up by prefix ID – works transparently in controllers via params[:id] User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> # # Plain integer primary key still works (fallback: true is the default) User.find(1) #=> # # With fallback: false, only prefix IDs are accepted # User.find(1) => raises PrefixedIds::Error ``` -------------------------------- ### Find Record by Prefixed ID (Alternative Methods) Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Use `find_by_prefix_id` or `find_by_prefix_id!` when the `find` override is disabled. The former returns nil if not found, while the latter raises an exception. ```ruby User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") # Returns a User or nil User.find_by_prefix_id!("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") # Raises an exception if not found ``` -------------------------------- ### Customize Prefixed ID Options Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Configure prefix, minimum length, and fallback behavior for prefixed IDs on a per-model basis. ```ruby class Account < ApplicationRecord has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: "", fallback: false end ``` -------------------------------- ### Encode Raw IDs to Prefix IDs with `.prefix_id` Source: https://context7.com/excid3/prefixed_ids/llms.txt Encode raw integer primary keys into their prefixed ID string representations using the class-level `.prefix_id` or `.prefix_ids` methods without needing a record instance. ```ruby User.prefix_id(1) #=> "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" User.prefix_ids([1, 2, 3]) #=> ["user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe", # "user_7aKmRsYpNqTx8cWz4bVhUi1LjFnOdGeC", # "user_2dPqXwBvMtJkLrGn9HyEaScOzUf5iRlC"] ``` -------------------------------- ### Decode Prefix IDs to Raw IDs with `.decode_prefix_id` Source: https://context7.com/excid3/prefixed_ids/llms.txt Decode one or more prefixed ID strings back into their original integer primary key format using `.decode_prefix_id` or `.decode_prefix_ids` without database interaction. ```ruby User.decode_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> 1 User.decode_prefix_ids([ "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe", "user_7aKmRsYpNqTx8cWz4bVhUi1LjFnOdGeC" ]) #=> [1, 2] ``` -------------------------------- ### Use Association Prefix ID Helpers Source: https://context7.com/excid3/prefixed_ids/llms.txt Models using `has_prefix_id` gain convenient reader and writer methods for foreign keys as prefix IDs on `belongs_to` associations, and a `#prefix_ids` method on `has_many` relations. ```ruby class Post < ApplicationRecord has_prefix_id :post belongs_to :user # gains :user_prefix_id reader and writer end ``` ```ruby post = Post.first # Reader – returns the associated user's prefix ID post.user_prefix_id #=> "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" ``` ```ruby # Writer – accepts a prefix ID to set the foreign key post.user_prefix_id = "user_7aKmRsYpNqTx8cWz4bVhUi1LjFnOdGeC" post.user_id #=> 2 ``` ```ruby # has_many relation – collect prefix IDs for all posts user = User.first user.posts.prefix_ids #=> ["post_3fNqLmRpKtVw9xZb2cYdAeJiGuSoHvXy", # "post_8gBnEaWzDcOmFrQsUlThPkIjYvNxCbLp"] ``` ```ruby # Create via prefix ID Post.create(user_prefix_id: "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") ``` -------------------------------- ### Composite Primary Key Support with Prefixed IDs Source: https://context7.com/excid3/prefixed_ids/llms.txt For models with composite primary keys, `encode` and `decode` methods handle arrays of IDs transparently, allowing prefixed IDs to be used seamlessly. ```ruby class CompoundPrimaryItem < ApplicationRecord self.primary_key = [:shop_id, :id] has_prefix_id :item end ``` ```ruby item = CompoundPrimaryItem.find([1, 42]) item.to_param #=> "item_8gBnEaWzDcOmFrQsUlThPkIjYvNxCbLp" ``` ```ruby CompoundPrimaryItem.find("item_8gBnEaWzDcOmFrQsUlThPkIjYvNxCbLp") #=> # ``` ```ruby CompoundPrimaryItem.decode_prefix_id("item_8gBnEaWzDcOmFrQsUlThPkIjYvNxCbLp") #=> [1, 42] ``` -------------------------------- ### Explicit Prefix ID Lookup with `.find_by_prefix_id` Source: https://context7.com/excid3/prefixed_ids/llms.txt Use `.find_by_prefix_id` (returns nil if not found) or `.find_by_prefix_id!` (raises `ActiveRecord::RecordNotFound`) for explicit lookups, especially when `override_find` is disabled. ```ruby # Returns the record or nil user = User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> # user = User.find_by_prefix_id("user_doesnotexist") #=> nil # Raises ActiveRecord::RecordNotFound if not found User.find_by_prefix_id!("user_doesnotexist") #=> ActiveRecord::RecordNotFound raised ``` -------------------------------- ### Set Global Salt for Prefixed IDs Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Define a global salt in an initializer to make your prefixed IDs unguessable. ```ruby # config/initializers/prefixed_ids.rb PrefixedIds.salt = "salt" ``` -------------------------------- ### .prefix_id / .prefix_ids — Encode IDs Without a Record Instance Source: https://context7.com/excid3/prefixed_ids/llms.txt Encodes raw integer primary keys into prefix IDs without requiring a record instance. `.prefix_id` encodes a single ID, while `.prefix_ids` encodes an array of IDs. ```APIDOC ## `.prefix_id` / `.prefix_ids` — Encode IDs Without a Record Instance Class-level helpers encode raw integer primary keys into prefix IDs without fetching the record. ```ruby User.prefix_id(1) #=> "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" User.prefix_ids([1, 2, 3]) #=> ["user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe", # "user_7aKmRsYpNqTx8cWz4bVhUi1LjFnOdGeC", # "user_2dPqXwBvMtJkLrGn9HyEaScOzUf5iRlC"] ``` ``` -------------------------------- ### Composite Primary Key Support Source: https://context7.com/excid3/prefixed_ids/llms.txt For models with composite primary keys, `encode` and `decode` handle arrays of IDs transparently. ```APIDOC ## Composite Primary Key Support ### Description Models with composite primary keys are supported by `PrefixedIds`. The `encode` and `decode` methods handle arrays of IDs correctly. ### Usage When `has_prefix_id` is used on a model with a composite primary key (e.g., `[:shop_id, :id]`), the `to_param` method will return the prefixed ID, and `find` will correctly decode it back into the array of primary key values. ``` -------------------------------- ### Disable `find` and `to_param` Overrides Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Configure `has_prefix_id` with `override_find: false` and `override_param: false` to disable automatic overrides. ```ruby class User < ApplicationRecord has_prefix_id :user, override_find: false, override_param: false end ``` -------------------------------- ### .find — Look Up a Record by Prefix ID Source: https://context7.com/excid3/prefixed_ids/llms.txt Finds a record using its prefix ID. When `override_find` is true (default), this method accepts either a prefix ID string or a plain integer primary key. The `fallback` option controls whether plain integer IDs are still accepted. ```APIDOC ## `.find` — Look Up a Record by Prefix ID When `override_find: true` (the default), the standard `.find` method accepts either a prefix ID string or a plain integer primary key. ```ruby # Look up by prefix ID – works transparently in controllers via params[:id] User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> # # Plain integer primary key still works (fallback: true is the default) User.find(1) #=> # # With fallback: false, only prefix IDs are accepted # User.find(1) => raises PrefixedIds::Error ``` ``` -------------------------------- ### Raise Error for Unknown Prefixes Source: https://context7.com/excid3/prefixed_ids/llms.txt When using `PrefixedIds.find`, an error is raised if the provided prefix is not registered. This ensures that only known resource types can be looked up. ```ruby PrefixedIds.find("unknown_abc123") #=> PrefixedIds::Error: Unable to find model with prefix `unknown`. # Available prefixes are: user, acct ``` -------------------------------- ### Find Record by Prefixed ID Source: https://github.com/excid3/prefixed_ids/blob/main/README.md When `find` override is enabled, you can use `User.find` with a prefixed ID to retrieve a record. ```ruby User.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> # ``` -------------------------------- ### .decode_prefix_id / .decode_prefix_ids — Decode a Prefix ID Back to a Raw ID Source: https://context7.com/excid3/prefixed_ids/llms.txt Decodes one or more prefix IDs back into their original integer primary keys without database interaction. `.decode_prefix_id` decodes a single ID, and `.decode_prefix_ids` decodes an array of IDs. ```APIDOC ## `.decode_prefix_id` / `.decode_prefix_ids` — Decode a Prefix ID Back to a Raw ID Decodes one or more prefix IDs into plain integer primary keys without hitting the database. ```ruby User.decode_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> 1 User.decode_prefix_ids([ "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe", "user_7aKmRsYpNqTx8cWz4bVhUi1LjFnOdGeC" ]) #=> [1, 2] ``` ``` -------------------------------- ### Set Per-Model Salt Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Apply a specific salt to a model for unique hashing, overriding any global salt. ```ruby class User has_prefix_id :user, salt: "usersalt" end ``` -------------------------------- ### Add Prefixed IDs Gem to Gemfile Source: https://context7.com/excid3/prefixed_ids/llms.txt Add the `prefixed_ids` gem to your application's Gemfile to enable its functionality. ```ruby # Gemfile gem 'prefixed_ids' ``` -------------------------------- ### Find Any Model by Prefixed ID Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Use `PrefixedIds.find` to locate a record by its prefixed ID, regardless of its model type. ```ruby PrefixedIds.find("user_5vJjbzXq9KrLEMm3") #=> # PrefixedIds.find("acct_2iAnOP0xGDYk6dpe") #=> # ``` -------------------------------- ### Association Prefix ID Helpers Source: https://context7.com/excid3/prefixed_ids/llms.txt When a model uses `has_prefix_id`, its `belongs_to` associations gain reader/writer methods for the foreign key as a prefix ID, and `has_many` relations gain a `#prefix_ids` method. ```APIDOC ## Association Prefix ID Helpers — `prefix_id` on `belongs_to` and `has_many` ### Description Models using `has_prefix_id` gain convenient methods for interacting with associated records using their prefixed IDs. ### `belongs_to` Association - **Reader**: `[association_name]_prefix_id` - Returns the associated record's prefix ID. - **Writer**: `[association_name]_prefix_id= ` - Accepts a prefix ID to set the foreign key. ### `has_many` Association - **`prefix_ids`**: Returns an array of prefix IDs for all associated records. ``` -------------------------------- ### .find_by_prefix_id / .find_by_prefix_id! — Explicit Prefix ID Lookup Source: https://context7.com/excid3/prefixed_ids/llms.txt Explicitly looks up a record by its prefix ID. Use these methods when `override_find` is disabled or for clarity. `.find_by_prefix_id` returns the record or `nil`, while `.find_by_prefix_id!` raises `ActiveRecord::RecordNotFound` if the record is not found. ```APIDOC ## `.find_by_prefix_id` / `.find_by_prefix_id!` — Explicit Prefix ID Lookup Use these methods when `override_find` is disabled, or when you want to be explicit that you are searching by prefix ID. ```ruby # Returns the record or nil user = User.find_by_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> # user = User.find_by_prefix_id("user_doesnotexist") #=> nil # Raises ActiveRecord::RecordNotFound if not found User.find_by_prefix_id!("user_doesnotexist") #=> ActiveRecord::RecordNotFound raised ``` ``` -------------------------------- ### PrefixedIds.find — Look Up Any Model by Prefix ID Source: https://context7.com/excid3/prefixed_ids/llms.txt Globally finds a model instance using its prefix ID, similar to GlobalIDs. This method resolves the prefix ID to its corresponding model without needing to know the class beforehand. ```APIDOC ## `PrefixedIds.find` — Look Up Any Model by Prefix ID Resolves a prefix ID to its corresponding model instance without knowing the class in advance. Works similarly to GlobalIDs. ```ruby PrefixedIds.find("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> # PrefixedIds.find("acct_2iAnOP0xGDYk6dpe") #=> # ``` ``` -------------------------------- ### Define Prefixed ID in Model Source: https://github.com/excid3/prefixed_ids/blob/main/README.md Add `has_prefix_id` to your model to automatically generate prefixed IDs. This should be placed before associations. ```ruby class User < ApplicationRecord has_prefix_id :user end ``` -------------------------------- ### PrefixedIds.find Source: https://context7.com/excid3/prefixed_ids/llms.txt Finds a record using a prefixed ID. Raises PrefixedIds::Error for unknown prefixes. ```APIDOC ## PrefixedIds.find ### Description Finds a record using a prefixed ID. Raises `PrefixedIds::Error` for unknown prefixes. ### Method `PrefixedIds.find(prefixed_id)` ### Parameters #### Path Parameters - **prefixed_id** (string) - Required - The prefixed ID to find. ### Response #### Success Response - Returns the found model instance. #### Error Response - **PrefixedIds::Error**: Raised if the prefix is unknown. ``` -------------------------------- ### Decode Prefix ID Globally Source: https://context7.com/excid3/prefixed_ids/llms.txt Use `PrefixedIds.decode_prefix_id` to convert a prefixed ID string back to its raw integer ID. This helper works for any registered prefix and passes through non-string inputs like integers or nil unchanged. ```ruby PrefixedIds.decode_prefix_id("user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe") #=> 1 ``` ```ruby PrefixedIds.decode_prefix_id(42) #=> 42 # integers pass through unchanged ``` ```ruby PrefixedIds.decode_prefix_id(nil) #=> nil ``` -------------------------------- ### PrefixedIds.decode_prefix_id Source: https://context7.com/excid3/prefixed_ids/llms.txt Decodes any registered prefix ID to its raw integer ID, regardless of model class. Returns the value unchanged if it is nil, already an integer, or not a recognised prefix. ```APIDOC ## PrefixedIds.decode_prefix_id — Global Decode Helper ### Description Decodes any registered prefix ID to its raw integer ID, regardless of model class. Returns the value unchanged if it is nil, already an integer, or not a recognised prefix. ### Method `PrefixedIds.decode_prefix_id(prefixed_id)` ### Parameters #### Path Parameters - **prefixed_id** (string | integer | nil) - Required - The prefixed ID or integer to decode. ### Response #### Success Response - Returns the raw integer ID, or the original value if it was nil or an integer. ``` -------------------------------- ### has_prefix_id — Declare a Prefixed ID on a Model Source: https://context7.com/excid3/prefixed_ids/llms.txt Adds prefix ID behavior to an ActiveRecord model. This method must be called before association declarations to ensure correct extension of `has_many` with prefix ID helpers. It accepts a prefix string and various options to customize the ID generation. ```APIDOC ## `has_prefix_id` — Declare a Prefixed ID on a Model Adds prefix ID behaviour to an ActiveRecord model. Must be called **before** association declarations so that `has_many` is correctly extended with prefix ID helpers. ```ruby class User < ApplicationRecord has_prefix_id :user # generates IDs like "user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe" has_many :posts end class Account < ApplicationRecord # All options shown: # prefix – the string prepended to the hashed ID (required) # minimum_length – minimum total length of the hashed portion (default: 24) # salt – per-model secret to make IDs unguessable (default: "") # override_find – patch .find to accept prefix IDs (default: true) # override_param – patch #to_param to return the prefix ID (default: true) # fallback – allow .find to also accept plain integer IDs (default: true) has_prefix_id :acct, minimum_length: 32, salt: "super_secret_salt", override_find: true, override_param: true, fallback: false end ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.