Ruby-code Flatten A Hash Of Arrays Into A Flat Array Of Hashes In Ruby Grouped data — { "ruby" => [post1, post2], "rails" => [post3] } — often needs to be turned back into a flat list where each item carries its group. It is the inverse
Rails-code Soft Delete Records In Rails Without A Gem Using Discarded_at Some records should disappear from the application without leaving the database — orders referenced by invoices, users referenced by audit logs. A nullable timestamp column and a default scope gives you soft deletion
Rails-code Add A Custom Computed Column To An Activeadmin Index Page The default ActiveAdmin index lists raw columns. Real admin screens need derived values — a total from associated rows, a status badge, a link to a related record — and the column block
Rails-code Test Action Mailer Content With Rspec Matchers In Rails Mailers break quietly — a renamed method in a template, a missing interpolation, a recipient list that silently resolves to nil. None of it surfaces until a customer says they never got the
Ruby-code Truncate A String At A Word Boundary In Ruby Without Cutting Mid Word Cutting a string at a fixed character count produces things like “The quick brown fo…”. Truncating at the last whole word before the limit reads properly and is what you want for previews,
Rails-code Serialize Query Params Back Into A Url String In Rails Filter forms, pagination links, and sort toggles all need to rebuild the current URL with one parameter changed and the rest preserved. Doing it with string concatenation breaks on the first array parameter
Ruby-code Extract Unique Values From An Array Of Hashes By A Key In Ruby You have a list of records and you want every distinct value of one field — all the account ids in a batch, every category present in a result set, the set of
Rails-code Bulk Insert Records With Insert_all And Skip Duplicates In Rails Importing thousands of rows one create! at a time issues one INSERT per record plus validations and callbacks. insert_all sends a single multi-row INSERT, which is orders of magnitude faster for imports and
Rails-code Prevent Duplicate Form Submissions In Rails With An Idempotency Key A user double-clicks Submit, or a mobile client retries after a timeout it thought had failed. Two identical requests arrive and you create two orders. Disabling the button in JavaScript does not solve
Rails-code Query Records Created Or Updated In The Last N Hours In Rails Dashboards, digest emails, and sync jobs all need “what changed recently”. Rails’ duration helpers plus a beginless or endless range make this readable and, crucially, index-friendly.
Ruby-code Retry A Block N Times With Exponential Backoff In Ruby Network calls, external APIs, and flaky I/O fail intermittently. Retrying immediately usually fails again — the remote service needs a moment. Exponential backoff spaces retries out so each attempt has a better chance
Rails-code Paginate Activerecord Results Without A Gem Using Limit And Offset Kaminari and Pagy are excellent, but a JSON API or an internal admin screen often needs nothing more than a page number and a page size. ActiveRecord gives you that in two methods.
Ruby-code Build A Hash From Two Parallel Arrays In Ruby Two arrays — one of keys, one of values, lined up by index — is a shape that comes out of CSV headers plus a row, form field names plus submitted values, and
Ruby-code Transpose A 2d Array Matrix In Ruby Flip a matrix over its diagonal — rows become columns and columns become rows. Ruby’s Array#transpose handles this in one call.
Ruby-code Memoize Expensive Method Calls In Ruby To Avoid Repeated Computation When a method performs an expensive operation — a database query, an API call, a complex calculation — calling it repeatedly wastes time and resources. Memoization caches the result on first call and