{"id":18712172,"url":"https://github.com/logandk/mongoid_denormalize","last_synced_at":"2025-04-09T14:14:19.256Z","repository":{"id":56884340,"uuid":"763737","full_name":"logandk/mongoid_denormalize","owner":"logandk","description":"Helper module for denormalizing association attributes in Mongoid models.","archived":false,"fork":false,"pushed_at":"2015-10-27T16:58:15.000Z","size":211,"stargazers_count":96,"open_issues_count":3,"forks_count":15,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-02T12:13:38.324Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://github.com/logandk/mongoid_denormalize","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/logandk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-07-08T11:36:04.000Z","updated_at":"2024-07-09T09:43:16.000Z","dependencies_parsed_at":"2022-08-20T13:10:51.970Z","dependency_job_id":null,"html_url":"https://github.com/logandk/mongoid_denormalize","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fmongoid_denormalize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fmongoid_denormalize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fmongoid_denormalize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/logandk%2Fmongoid_denormalize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/logandk","download_url":"https://codeload.github.com/logandk/mongoid_denormalize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054193,"owners_count":21039952,"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-07T12:40:53.032Z","updated_at":"2025-04-09T14:14:19.236Z","avatar_url":"https://github.com/logandk.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"Mongoid::Denormalize\n====================\n\n*Note: I am no longer actively maintaining this module, and I do not know whether it is compatible with newer versions of mongoid. If you would like to take ownership of this repository, please let me know.*\n\n\nHelper module for denormalizing association attributes in Mongoid models. Why denormalize? Read *[A Note on Denormalization](http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails#MongoDBDataModelingandRails-ANoteonDenormalization)*.\n\nExtracted from [coookin'](http://coookin.com), where it is used in production.\n\n\nInstallation\n------------\n\nAdd the gem to your Bundler `Gemfile`:\n\n    gem 'mongoid_denormalize'\n\nOr install with RubyGems:\n\n    $ gem install mongoid_denormalize\n\n\nUsage\n-----\n\nIn your model:\n\n    # Include the helper method\n    include Mongoid::Denormalize\n    \n    # Define your denormalized fields\n    denormalize :name, :email, :from =\u003e :user\n\n\nExample\n-------\n\n    class User\n      include Mongoid::Document\n      include Mongoid::Denormalize\n\n      has_many :comments\n      has_many :moderated_comments, :class_name =\u003e \"Comment\", :inverse_of =\u003e :moderator\n\n      field :name\n      field :email\n      \n      denormalize :name, :email, :to =\u003e :comments\n    end\n    \n    class Comment\n      include Mongoid::Document\n      include Mongoid::Denormalize\n\n      belongs_to :user\n      belongs_to :moderator, :class_name =\u003e \"User\", :inverse_of =\u003e :moderated_comments\n\n      field :body\n      \n      denormalize :name, :email, :from =\u003e :user\n    end\n    \n    \u003e\u003e user = User.create(:name =\u003e \"John Doe\", :email =\u003e \"john@doe.com\")\n    \u003e\u003e comment = Comment.create(:body =\u003e \"Lorem ipsum...\", :user =\u003e user)\n    \u003e\u003e user.comments \u003c\u003c comment\n    \u003e\u003e comment.user_name\n    \"John Doe\"\n    \u003e\u003e comment.user_email\n    \"john@doe.com\"\n    \u003e\u003e user.update_attributes(:name =\u003e \"Bill\")\n    \u003e\u003e comment.user_name\n    \"Bill\"\n\n\nOptions\n-------\n\nDenormalization can happen in either or both directions. When using the `:from` option, the associated objects will fetch the values from\nthe parent. When using the `:to` option, the parent will push the values to its children.\n\n    # Basic denormalization. Will set the user_name attribute of \"self.comments\" to the value of \"self.name\".\n    denormalize :name, :to =\u003e :comments\n\n    # Multiple children. Will set the user_name attribute of \"self.posts\" and \"self.comments\" with \"self.name\".\n    denormalize :name, :to =\u003e [:posts, :comments]\n\n    # Basic denormalization, obeying :inverse_of. Will set the moderator_name attribute of \"self.moderated_comments\"\n    denormalize :name, :email, :to =\u003e :moderated_comments\n\n    # With custom field name prefix. Will set the commenter_name attribute of \"self.comments\" (takes precedence over\n    # inverse_of).\n    denormalize :name, :to =\u003e :comments, :as =\u003e :commenter\n    denormalize :name, :from =\u003e :comments, :as =\u003e :commenter\n\n    # Basic denormalization. Will set the user_name attribute with the associated user's name.\n    denormalize :name, :from =\u003e :user\n\n    # Multiple fields. Will set the user_name and user_email attributes with the associated user's name and email.\n    denormalize :name, :email, :from =\u003e :user\n\n    # Basic denormalization. Will set the moderator_name attribute with the associated author user's name.\n    denormalize :name, :from =\u003e :moderator\n\nWhen using `:from`, if the type of the denormalized field is anything but `String` (the default),\nyou must specify the type with the `:type` option.\n\n    # in User\n    field :location, :type =\u003e Array\n    denormalize :location, :to =\u003e :posts\n    \n    # in Post\n    denormalize :location, :type =\u003e Array, :from =\u003e :user\n\nA few notes on behavior:\n\n`:from` denormalizations are processed as `before_save` callbacks.\n\n`:to` denormalizations are processed as `after_save` callbacks.\n\nWith `:to`, validations are not run on the object(s) containing the denormalized field(s) when they are saved.\n\nRake tasks\n----------\n\nA rake task is included for verifying and repairing inconsistent denormalizations. This might be useful your records may be modified\nwithout Mongoid, or if you are using relational associations in combination with embedded records (see *Known issues*).\n\n    rake db:denormalize\n    \nThis task will only update records that have outdated denormalized fields.\n\n\nKnown issues\n------------\n\n**Relational associations in combination with embedded records**\n\nIt is not recommended nor supported to use mongoid_denormalize to perform denormalization to embedded records through a relational association because\n MongoDB/Mongoid do not support direct access to embedded fields via a relational association.\n\nSo, if User has_many :posts and User has_many :comments, but Comments are embedded_in :post, a user can't directly access a comment.\n\nContributing\n-------\n\nClone the repository and run Bundler with `bundle install`. Use `rake -T` to view available rake tasks.\n\nYou can also use `bundle exec guard` for test driven development, if desired.\n\nA MongoDB server is required to run the specs. Docker is an easy option:\n\n    docker run --name mongo3 -d -p 27017:27017 mongo\n\nContributors\n-------\n* hubsmoke (https://github.com/hubsmoke)\n* Leo Lou (https://github.com/l4u)\n* Austin Bales (https://github.com/arbales)\n* Isaac Cambron (https://github.com/icambron)\n* Shannon Carey (https://github.com/rehevkor5)\n* Sebastien Azimi (https://github.com/suruja)\n* HuffMoody (https://github.com/HuffMoody)\n\nCredits\n-------\n\nCopyright (c) 2010 Logan Raarup, released under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogandk%2Fmongoid_denormalize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flogandk%2Fmongoid_denormalize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flogandk%2Fmongoid_denormalize/lists"}