Skip to content

Instantly share code, notes, and snippets.

@kshsieh
Created November 21, 2013 22:26
Show Gist options
  • Save kshsieh/7590889 to your computer and use it in GitHub Desktop.
Save kshsieh/7590889 to your computer and use it in GitHub Desktop.
dual polymorphic joins table
class Groupable
has_many :group_rules, as: :grouped
has_many :rule_objects, through: :group_rules
end
class GroupRules
belongs_to :grouped, polymorphic: true
belongs_to :rulable, polymoprhic: true
end
class Country
has_many rules, as: :rulable
end
# trying to end up with something like groupable.rule_objects return an array of objects like Country, State, City, or Residency that can match up against a user
# but getting an error ActiveRecord::HasManyThroughAssociationPolymorphicSourceError: Cannot have a has_many :through association 'Groupable#rule_objects' on the polymorphic object 'Rulable#rulable'.
@ScotterC
Copy link

class Groupable
  has_many :group_rule_sets, as: :grouped
  has_many :rules, through: :groupable_group_rules, source: :group_rules
end

class GroupRuleSet
  belongs_to :grouped, polymorphic: true
  has_many :group_rules
end

class GroupRule
  belongs_to :group_rule_set
  belongs_to :rulable, polymorphic: true
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment