{"id":19939186,"url":"https://github.com/zapnap/mongoid-embedded-errors","last_synced_at":"2025-05-03T14:32:45.265Z","repository":{"id":5773637,"uuid":"6987267","full_name":"zapnap/mongoid-embedded-errors","owner":"zapnap","description":"Easily bubble up errors from embedded documents in Mongoid.","archived":false,"fork":false,"pushed_at":"2012-12-03T18:39:07.000Z","size":102,"stargazers_count":0,"open_issues_count":0,"forks_count":31,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T17:04:23.821Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.markbates.com","language":"Ruby","has_issues":false,"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/zapnap.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":"2012-12-03T18:30:32.000Z","updated_at":"2021-05-17T09:07:39.000Z","dependencies_parsed_at":"2022-08-31T02:01:10.182Z","dependency_job_id":null,"html_url":"https://github.com/zapnap/mongoid-embedded-errors","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapnap%2Fmongoid-embedded-errors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapnap%2Fmongoid-embedded-errors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapnap%2Fmongoid-embedded-errors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zapnap%2Fmongoid-embedded-errors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zapnap","download_url":"https://codeload.github.com/zapnap/mongoid-embedded-errors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252203433,"owners_count":21710963,"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":[],"created_at":"2024-11-12T23:45:17.533Z","updated_at":"2025-05-03T14:32:44.962Z","avatar_url":"https://github.com/zapnap.png","language":"Ruby","readme":"# Mongoid::EmbeddedErrors\n\nEasily bubble up errors from embedded documents in Mongoid.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'mongoid-embedded-errors'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install mongoid-embedded-errors\n\n## Usage\n\nEmbedded documents in Mongoid can be really useful. However, when one of those embedded documents is invalid, Mongoid spits up completely useless errors.\n\nLet's look an example. Here we have an `Article` which `embeds_many :pages`. A `Page` `embeds_many :sections`.\n\n```ruby\nclass Article\n  include Mongoid::Document\n\n  field :name, type: String\n  field :summary, type: String\n  validates :name, presence: true\n  validates :summary, presence: true\n\n  embeds_many :pages\nend\n\nclass Page\n  include Mongoid::Document\n\n  field :title, type: String\n  validates :title, presence: true\n\n  embedded_in :article, inverse_of: :pages\n  embeds_many :sections\nend\n\nclass Section\n  include Mongoid::Document\n\n  field :header, type: String\n  field :body, type: String\n  validates :header, presence: true\n\n  embedded_in :page, inverse_of: :sections\nend\n```\n\nIf we were to create an invalid `Article` with an invalid `Page` and tried to validate it the errors we see would not be very helpful:\n\n```ruby\narticle = Article.new(pages: [Page.new])\narticle.valid? # =\u003e false\n\narticle.error.messages\n# =\u003e {:name=\u003e[\"can't be blank\"], :summary=\u003e[\"can't be blank\"], :pages=\u003e[\"is invalid\"]}\n```\n\nWhy was the `Page` invalid? Who knows! But, if we include the `Mongoid::EmbeddedErrors` module we get much better error messaging:\n\n```ruby\nclass Article\n  include Mongoid::Document\n  include Mongoid::EmbeddedErrors\n\n  field :name, type: String\n  field :summary, type: String\n  validates :name, presence: true\n  validates :summary, presence: true\n\n  embeds_many :pages\nend\n\narticle = Article.new(pages: [Page.new(sections: [Section.new])])\narticle.valid? # =\u003e false\n\narticle.error.messages\n# =\u003e {:name=\u003e[\"can't be blank\"], :summary=\u003e[\"can't be blank\"], :pages=\u003e[{\"5086ec0d421aa94f0f000002\"=\u003e{:title=\u003e[\"can't be blank\"], :sections=\u003e[{\"5086ec0d421aa94f0f000001\"=\u003e{:header=\u003e[\"can't be blank\"]}}]}}]}\n```\n\nNow, isn't that much nicer? Yeah, I think so to.\n\n## Contributing\n\n1. Fork it\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 new Pull Request\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzapnap%2Fmongoid-embedded-errors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzapnap%2Fmongoid-embedded-errors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzapnap%2Fmongoid-embedded-errors/lists"}