{"id":19404939,"url":"https://github.com/samesystem/activerecord-relatives","last_synced_at":"2025-02-25T00:44:32.376Z","repository":{"id":40218296,"uuid":"201913471","full_name":"samesystem/activerecord-relatives","owner":"samesystem","description":null,"archived":false,"fork":false,"pushed_at":"2023-03-08T20:14:15.000Z","size":70,"stargazers_count":0,"open_issues_count":6,"forks_count":1,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-01-07T13:12:10.737Z","etag":null,"topics":[],"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/samesystem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-12T11:01:40.000Z","updated_at":"2020-08-14T02:56:48.000Z","dependencies_parsed_at":"2024-11-10T11:36:54.728Z","dependency_job_id":"05bf2de8-1d07-4861-bba4-67d8be2718b8","html_url":"https://github.com/samesystem/activerecord-relatives","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samesystem%2Factiverecord-relatives","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samesystem%2Factiverecord-relatives/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samesystem%2Factiverecord-relatives/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samesystem%2Factiverecord-relatives/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samesystem","download_url":"https://codeload.github.com/samesystem/activerecord-relatives/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240582047,"owners_count":19824145,"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-10T11:36:43.998Z","updated_at":"2025-02-25T00:44:32.315Z","avatar_url":"https://github.com/samesystem.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActiveRecord::Relatives\n\n[![Build Status](https://travis-ci.org/samesystem/activerecord-relatives.svg?branch=master)](https://travis-ci.org/samesystem/activerecord-relatives)\n[![codecov](https://codecov.io/gh/samesystem/activerecord-relatives/branch/master/graph/badge.svg)](https://codecov.io/gh/samesystem/activerecord-relatives)\n[![Documentation](https://readthedocs.org/projects/ansicolortags/badge/?version=latest)](https://samesystem.github.io/activerecord-relatives)\n\nThis tool will help you find all the associations and their ids related with given model.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord-relatives'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install activerecord-relatives\n\n## Usage\n\nThis tool will find all ids related with given model. Let's say your models look something like this:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  has_many :comments\n  has_many :posts\nend\n\nclass Post \u003c ActiveRecord::Base\n  belongs_to :author, class_name: 'User'\nend\n\nclass Comment \u003c ActiveRecord::Base\n  belongs_to :post\nend\n```\n\nand your data looks like this:\n\n```ruby\nuser1 = User.create! #=\u003e #\u003cUser id: 1\u003e\npost1 = Post.create(author: user1) #=\u003e #\u003cPost id: 1 ...\u003e\npost2 = Post.create(author: user1) #=\u003e #\u003cPost id: 2 ...\u003e\nComment.create!(post: post1) #=\u003e #\u003cComment id: 1 ...\u003e\n\nuser2 = User.create! #=\u003e #\u003cUser id: 2\u003e\npost3 = Post.create(author: user1) #=\u003e #\u003cPost id: 3 ...\u003e\nComment.create!(post: post3) #=\u003e #\u003cComment id: 3 ...\u003e\n```\n\nthen `ActiveRecord::Relatives` will return all ids which are somehow related for a given record:\n\n```ruby\nActiveRecord::Relatives.call(user1) # =\u003e { Post =\u003e [1, 2], Comment =\u003e [1] }\n```\n\n## Requirements\n\nIn order to make this tool work you need to have your associations properly set\n\n## Usage examples\n\nThis tool can be handy for various tasks, like:\n\n* removing records with all associations\n* detecting data which has missing associations (like, after incomplete delete)\n* analyzing dependencies\n* detecting circular dependencies\n* detecting out of sync objects (like, objects which are related with multiple users, but they shouldn't)\n* detecting god models\n* generating UML diagrams\n* you name it :)\n\n### Removing records with all associations\n\n```ruby\nActiveRecord::Base.transaction do\n  ActiveRecord::Relatives.call(user1).each do |model, data|\n    model.unscoped.where(id: data.ids).delete_all\n  end\nend\n```\n\n### Detecting out of sync objects\n\n```ruby\nrecords_without_user = []\nActiveRecord::Relatives.call(User.all.unscoped).each do |model, data|\n  records_without_user \u003c\u003c model.unscoped.where.not(id: data.ids)\nend\n```\n\n### Analyzing dependencies\n\n```ruby\npost_dependency = ActiveRecord::Relatives\n  .call(user1)\n  .dependencies\n  .detect { |dependency| dependency.key?(Post) }\npost_dependency.depends_on #=\u003e [User, Image, ...]\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/samesystem/activerecord-relatives. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the ActiveRecord::Relatives project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/samesystem/activerecord-relatives/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamesystem%2Factiverecord-relatives","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamesystem%2Factiverecord-relatives","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamesystem%2Factiverecord-relatives/lists"}