{"id":13879315,"url":"https://github.com/y9v/activerecord-jsonb-associations","last_synced_at":"2026-03-10T11:04:11.042Z","repository":{"id":62552840,"uuid":"108323822","full_name":"y9v/activerecord-jsonb-associations","owner":"y9v","description":"Use PostgreSQL JSONB fields to store foreign ids on your models.","archived":false,"fork":false,"pushed_at":"2018-11-09T20:37:02.000Z","size":306,"stargazers_count":70,"open_issues_count":2,"forks_count":25,"subscribers_count":3,"default_branch":"master","last_synced_at":"2026-01-14T10:42:39.953Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/y9v.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-25T20:41:47.000Z","updated_at":"2025-02-13T11:17:12.000Z","dependencies_parsed_at":"2022-11-03T04:15:16.666Z","dependency_job_id":null,"html_url":"https://github.com/y9v/activerecord-jsonb-associations","commit_stats":null,"previous_names":["lebedev-yury/activerecord-jsonb-associations"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/y9v/activerecord-jsonb-associations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y9v%2Factiverecord-jsonb-associations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y9v%2Factiverecord-jsonb-associations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y9v%2Factiverecord-jsonb-associations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y9v%2Factiverecord-jsonb-associations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/y9v","download_url":"https://codeload.github.com/y9v/activerecord-jsonb-associations/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/y9v%2Factiverecord-jsonb-associations/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30331644,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2024-08-06T08:02:17.063Z","updated_at":"2026-03-10T11:04:11.024Z","avatar_url":"https://github.com/y9v.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# activerecord-json-associations\n\n[![Gem Version](https://badge.fury.io/rb/activerecord-jsonb-associations.svg)](https://badge.fury.io/rb/activerecord-jsonb-associations)\n\nUse PostgreSQL JSONB fields to store association information of your models.\n\nThis gem was created as a solution to this [task](http://cultofmartians.com/tasks/active-record-jsonb-associations.html) from [EvilMartians](http://evilmartians.com).\n\n**Requirements:**\n\n- PostgreSQL (\u003e= 9.6)\n\n## Usage\n\n### One-to-one and One-to-many associations\n\nYou can store all foreign keys of your model in one JSONB column, without having to create multiple columns:\n\n```ruby\nclass Profile \u003c ActiveRecord::Base\n  # Setting additional :store option on :belongs_to association\n  # enables saving of foreign ids in :extra JSONB column \n  belongs_to :user, store: :extra\nend\n\nclass SocialProfile \u003c ActiveRecord::Base\n  belongs_to :user, store: :extra\nend\n\nclass User \u003c ActiveRecord::Base\n  # Parent model association needs to specify :foreign_store\n  # for associations with JSONB storage\n  has_one :profile, foreign_store: :extra\n  has_many :social_profiles, foreign_store: :extra\nend\n```\n\nForeign keys for association on one model have to be unique, even if they use different store column.\n\nYou can also use `add_references` in your migration to add JSONB column and index for it (if `index: true` option is set):\n\n```ruby\nadd_reference :profiles, :users, store: :extra, index: true\n```\n\n### Many-to-many associations\n\nYou can also use JSONB columns on 2 sides of a HABTM association. This way you won't have to create a join table.\n\n```ruby\nclass Label \u003c ActiveRecord::Base\n  # extra['user_ids'] will store associated user ids\n  has_and_belongs_to_many :users, store: :extra\nend\n\nclass User \u003c ActiveRecord::Base\n  # extra['label_ids'] will store associated label ids\n  has_and_belongs_to_many :labels, store: :extra\nend\n```\n\n#### Performance\n\nCompared to regular associations, fetching models associated via JSONB column has no drops in performance.\n\nGetting the count of connected records is ~35% faster with associations via JSONB (tested on associations with up to 10 000 connections).\n\nAdding new connections is slightly faster with JSONB, for scopes up to 500 records connected to another record (total count of records in the table does not matter that much. If you have more then ~500 records connected to one record on average, and you want to add new records to the scope, JSONB associations will be slower then traditional:\n\n\u003cimg src=\"https://github.com/lebedev-yury/activerecord-jsonb-associations/blob/master/doc/images/adding-associations.png?raw=true | width=500\" alt=\"JSONB HAMTB is slower on adding associations\" width=\"600\"\u003e\n\nOn the other hand, unassociating models from a big amount of associated models if faster with JSONB HABTM as the associations count grows:\n\n\u003cimg src=\"https://github.com/lebedev-yury/activerecord-jsonb-associations/blob/master/doc/images/deleting-associations.png?raw=true | width=500\" alt=\"JSONB HAMTB is faster on removing associations\" width=\"600\"\u003e\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord-jsonb-associations'\n```\n\nAnd then execute:\n\n```bash\n$ bundle install\n```\n\n## Developing\n\nTo setup development environment, just run:\n\n```bash\n$ bin/setup\n```\n\nTo run specs:\n\n```bash\n$ bundle exec rspec\n```\n\nTo run benchmarks (that will take a while):\n\n```bash\n$ bundle exec rake benchmarks:habtm\n```\n\n## License\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fy9v%2Factiverecord-jsonb-associations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fy9v%2Factiverecord-jsonb-associations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fy9v%2Factiverecord-jsonb-associations/lists"}