{"id":25619327,"url":"https://github.com/hunterae/has_relationship","last_synced_at":"2026-03-16T15:39:21.284Z","repository":{"id":56876031,"uuid":"1514392","full_name":"hunterae/has_relationship","owner":"hunterae","description":"has_relationship is an easy way to instantly create an association between any two database tables","archived":false,"fork":false,"pushed_at":"2011-07-19T18:48:01.000Z","size":106,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-23T05:43:26.587Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hunterae.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-03-23T01:17:49.000Z","updated_at":"2019-08-13T14:43:09.000Z","dependencies_parsed_at":"2022-08-20T23:10:23.904Z","dependency_job_id":null,"html_url":"https://github.com/hunterae/has_relationship","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/hunterae/has_relationship","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hunterae%2Fhas_relationship","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hunterae%2Fhas_relationship/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hunterae%2Fhas_relationship/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hunterae%2Fhas_relationship/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hunterae","download_url":"https://codeload.github.com/hunterae/has_relationship/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hunterae%2Fhas_relationship/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267423948,"owners_count":24084959,"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","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"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":[],"created_at":"2025-02-22T06:16:56.454Z","updated_at":"2026-03-16T15:39:16.256Z","avatar_url":"https://github.com/hunterae.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= HasRelationship\n\nThis gem was based on insight provided by the article http://blog.hasmanythrough.com/2006/4/3/polymorphic-through.\nIt was developed because of a lack of solutions to be able to handle double polymorphism in rails. In other words,\nthere was no way to create an xref table (the middle table in a many-to-many relationship) that joined two unknown\ntables together. There are obvious reasons for these limitations in rails, but nevertheless, a solution was needed.\n\nHasRelationship hopefully is that solution. The user runs a migration that generates the \"relationships\" table. That\ntable is then used to join any two tables together and create a relationship between them. So now, when you want to \nadd a relationship between class 1 and class 2, you won't have to generate your own custom intermediate xref table\neach time; the \"relationships\" table can be reused for each of your custom relationships.\n\n== Installation\n\n=== Rails 3.0\n\nAdd the following to your Gemfile:\n  \n  gem 'has-relationship'\n\n==== Post Installation\n\n1. rails generate has_relationship:migration\n2. rake db:migrate\n\n== Usage\n\n    class User \u003c ActiveRecord::Base\n      # Create a has-one-through relationship with the \"tasks\" table (Task class) through the \"relationships\" table\n      has_relationship :task\n      \n      # Alternate approach to create a has-one-through relationship with the \"tasks\" table (Task class) through the \"relationships\" table\n      has_relationship \"task\"\n      \n      # Alternate approach to create a has-one-through relationship with the \"tasks\" table (Task class) through the \"relationships\" table\n      has_relationship Task\n      \n      # Alternate approach to create a has-one-through relationship with the \"tasks\" table (Task class) through the \"relationships\" table\n      has_relationship :tasks, :singular =\u003e true\n      \n      \n      \n      # Create a has-many-through relationship with the \"tasks\" table (Task class) through the \"relationships\" table\n      has_relationship :tasks\n      \n      # Create a has-many-through relationship called \"other_tasks\" with the \"tasks\" table (Task class) through the \"relationships\" table.\n      # Please note the addition of the :relationship attribute. This is optional but highly recommended, particularly when you're going\n      # to be declaring a \"has_inverse_relationship\" on the Task class, as shown below. This parameter sets the \"relationship\" field in\n      # the relationship table.\n      # If :relationship had not been set here, it still would have defaulted to \"User_OtherTask\"; the benefit however of manually declaring this\n      # is that it clear exactly what to name the relationship in your use of \"has_inverse_relationship\".\n      has_relationship :other_tasks, :class_name =\u003e \"Task\", :relationship =\u003e \"User_OtherTask\"\n      \n      # Alternate approach to create a has-many-through relationship called \"other_tasks\" with the \"tasks\" table (Task class) through the \"relationships\" table\n      has_relationship :tasks, :as =\u003e :other_tasks, :relationship =\u003e \"User_OtherTask\"\n    end\n    \n    class Task \u003c ActiveRecord::Base\n      # Create a has-one-through relationship with the \"users\" table (User class) through the \"relationships\" table\n      has_inverse_relationship :user\n    \n      # Alternate approach to create a has-one-through relationship with the \"users\" table (User class) through the \"relationships\" table\n      has_inverse_relationship \"user\"\n    \n      # Alternate approach to create a has-one-through relationship with the \"users\" table (User class) through the \"relationships\" table\n      has_inverse_relationship User\n    \n      # Alternate approach to create a has-one-through relationship with the \"user\" table (User class) through the \"relationships\" table\n      has_inverse_relationship :users, :singular =\u003e true\n    \n    \n    \n      # Create a has-many-through relationship with the \"users\" table (User class) through the \"relationships\" table\n      has_inverse_relationship :users\n    \n      # Create a has-many-through relationship called \"other_users\" with the \"users\" table (User class) through the \"relationships\" table\n      # Please note that here we specify the exact same :relationship as was used in the User class in its call to \"has_relationship\".\n      has_inverse_relationship :other_users, :class_name =\u003e \"User\", :relationship =\u003e \"User_OtherTask\"\n    \n      # Alternate approach to create a has-many-through relationship called \"other_users\" with the \"users\" table (User class) through the \"relationships\" table\n      has_inverse_relationship :users, :as =\u003e :other_users, :relationship =\u003e \"User_OtherTask\"\n      \n      \n      \n      # Create a has-one-through relationship with the \"assignments\" table (Assignment class) through the \"relationships\" table\n      has_inverse_relationship :assignment\n      \n      # Create a has-many-through relationship with the \"stories\" table (Story Class) through the \"relationships\" table\n      has_relationship :stories\n    end\n    \n    class Assignment \u003c ActiveRecord::Base\n      # Create a has-many-through relationship with the \"tasks\" table (Task class) through the \"relationships\" table,\n      #  and create another has-many-through relationship with the \"stories\" table (Story class) through the \"relationships\" table\n      has_relationship [:tasks, :stories]\n      \n      # Create a has-many-through relationship called \"other_tasks\" with the \"tasks\" table (Task class) through the \"relationships\" table\n      has_relationship :tasks, :as =\u003e :other_tasks\n    end\n    \n    class Story \u003c ActiveRecord::Base\n      # Create a has-one-through relationship called \"parent\" with the \"tasks\" table (Task class) through the \"relationships\" table.\n      has_inverse_relationship :tasks, :as =\u003e :parent, :relationship =\u003e \"Task_Story\"\n      \n      # Create a has-one-through relationship with the \"tasks\" table (Task class) through the \"relationships\" table.\n      has_inverse_relationship :task\n    end\n\n    @user = User.new(:name =\u003e \"Bobby\")\n    @user.task = Task.new\n    @user.tasks.build\n    @user.other_tasks \u003c\u003c Task.new\n    @user.save\n\n    @assignment = Assignment.new\n    @assignment.tasks \u003c\u003c Task.create\n    @assignment.stories.build\n    @assignment.other_tasks \u003c\u003c Task.new\n    @assignment.save\n    \n    @story = Story.new\n    @story.parent = Task.create\n    @story.task = Task.new\n    @story.save\n\nCopyright (c) 2011 Andrew Hunter (http://github.com/hunterae) and Captico LLC. (http://captico.com/), released under the MIT license\n\n== Special Thanks\n\nHasRelationship was aided by some of the insight provided in the article http://blog.hasmanythrough.com/2006/4/3/polymorphic-through written by Josh Susser. Also many thanks to the ActsAsTaggableOn team[https://github.com/mbleigh/acts-as-taggable-on], as I have used your gem as a jumping point for writing my own, given the similar nature of the task at hand.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhunterae%2Fhas_relationship","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhunterae%2Fhas_relationship","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhunterae%2Fhas_relationship/lists"}