{"id":16102359,"url":"https://github.com/sshaw/keep_defaults","last_synced_at":"2026-04-28T08:03:05.746Z","repository":{"id":56879975,"uuid":"244242401","full_name":"sshaw/keep_defaults","owner":"sshaw","description":"Prevent ActiveRecord attributes for not null columns with default values from being set to nil.","archived":false,"fork":false,"pushed_at":"2020-04-08T05:35:24.000Z","size":12,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-25T04:09:27.331Z","etag":null,"topics":["activerecord","activerecord-models","default-value","rails","ruby"],"latest_commit_sha":null,"homepage":null,"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/sshaw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-01T23:48:03.000Z","updated_at":"2020-04-08T05:35:26.000Z","dependencies_parsed_at":"2022-08-20T11:40:28.687Z","dependency_job_id":null,"html_url":"https://github.com/sshaw/keep_defaults","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/sshaw/keep_defaults","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Fkeep_defaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Fkeep_defaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Fkeep_defaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Fkeep_defaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sshaw","download_url":"https://codeload.github.com/sshaw/keep_defaults/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sshaw%2Fkeep_defaults/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32371673,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"online","status_checked_at":"2026-04-28T02:00:07.250Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["activerecord","activerecord-models","default-value","rails","ruby"],"created_at":"2024-10-09T18:53:38.507Z","updated_at":"2026-04-28T08:03:05.545Z","avatar_url":"https://github.com/sshaw.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keep Defaults\n\n[![Build Status](https://travis-ci.org/sshaw/keep_defaults.svg?branch=master)](https://travis-ci.org/sshaw/keep_defaults)\n\nPrevent ActiveRecord attributes for `not null` columns with default values from being set to `nil`.\n\nWhy is this necessary? Take this example:\n```rb\nclass OrderItem \u003c ApplicationRecord\n  # Has column: total numeric(11,2) not null default 0\n  # Validation etc...\nend\n\nclass Order \u003c ApplicationRecord\n  # Has columns: total and taxes, both numeric(11,2) not null default 0\n  # Validation etc...\n\n  has_many :order_items\n\n  def total\n    order_items.sum(\u0026:total) + taxes\n  end\nend\n```\n\nThe columns have a default value of `0`, but the attributes can still be set to `nil`.\nThis can make for code that is far from bulletproof:\n```rb\no = Order.new\no.total  # 0\no.taxes = nil\no.total  # 💥 TypeError: nil can't be coerced into Fixnum\n```\n\nTo fix you can do something like:\n```rb\nclass Order \u003c ApplicationRecord\n  def total\n    order_items.sum(\u0026:total) + taxes.to_f\n  end\nend\n```\n\nBut `OrderItem#total` can be set to `nil` too. You can do:\n```rb\nclass OrderItem \u003c ApplicationRecord\n  def total\n    super || 0\n  end\nend\n```\n\nBut what about the other contexts in which these can be called or the other attributes you may have? This can get tedious.\n\nWith Keep Defaults:\n```rb\nclass ApplicationRecord \u003c ActiveRecord::Base\n  self.abstract_class = true\n  # Must come after setting abstract_class\n  include KeepDefaults\nend\n```\n\n```rb\no = Order.new\no.total  # 0\no.taxes = nil\no.total  # 0\no.taxes  # 0\n```\n\nNow if an attribute is set to `nil` it will retain —or be returned to— its default value instead.\n\n## Installation\n\nAdd this line to your application's `Gemfile`:\n\n```rb\ngem \"keep_defaults\"\n```\n\nOr\n\n```rb\ngem install keep_defaults\n```\n\n## Usage\n\nTo use everywhere add to `ApplicationRecord`:\n\n```rb\nclass ApplicationRecord \u003c ActiveRecord::Base\n  self.abstract_class = true\n  # Must come after setting abstract_class\n  include KeepDefaults\nend\n```\n\nTo use for a specific class add it directly to that class:\n\n```rb\nclass Order \u003c ApplicationRecord\n  include KeepDefaults\nend\n```\n\nIf your class sets its table via `table_name` then `include KeepDefaults` must come after that.\n\n### Using With an Existing Column\n\nTo ensure that an attribute always returns its default value you must make sure its DB column does not allow `null` and has a default.\n\nFor example, given the column `orders.taxes` that does not meet these requirements, you can add a migration containing the following:\n```rb\ndef change\n  change_column :orders, :taxes, :integer, :null =\u003e false, :default =\u003e 0\nend\n```\n\n### Known Issues\n\n#### Classes That Explicitly Set `table_name` **and** Have an Ancestor Class That `include`s `KeepDefaults`\n\nIn this case `include KeepDefaults` must be taken out of the ancestor classes in added to all the subclasses.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsshaw%2Fkeep_defaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsshaw%2Fkeep_defaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsshaw%2Fkeep_defaults/lists"}