Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:45:01+00:00 2026-06-14T03:45:01+00:00

I’m working on an engine where any model can have a has_many association with

  • 0

I’m working on an engine where any model can have a has_many association with Permit as Permissible:

class Permit < ActiveRecord::Base
  belongs_to :permissible, polymorphic: true
end

module Permissible
  def self.included(base)
    base.class_eval do
    has_many :permits, as: :permissible
  end
end

class Group < ActiveRecord::Base
  include Permissible
end

class GroupAllocation < ActiveRecord::Base
  belongs_to :person
  belongs_to :group
end

class Person < ActiveRecord::Base
  include Permissible
  has_many :group_allocations
  has_many :groups, through: :group_allocations
end

class User < ActiveRecord::Base
  belongs_to :person
end

So, Group has_many :permits and Person has_many :permits. What I am trying to do is dynamically create associations on User that uses the permits association as a source, and chain associations on other models down to User by doing the same. This can be done manually (in rails 3.1+) with:

class Person
  has_many :group_permits, through: :person, source: :permits
end

class User
  has_many :person_permits, through: :person, source: :permits, class_name: Permit
  has_many :person_group_permits, through: :person, source: :group_permits, class_name: Permit
end

However, in practice, Permissible will be included on many models, so I’m trying to write a class method on User (actually within another module, but no need to confuse things more) that can traverse User.reflect_on_all_associations and create an array of new associations, which may be many associations deep each.

Looking for input on how to do this cleanly in rails 3.2.8.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:45:02+00:00Added an answer on June 14, 2026 at 3:45 am

    Here is how I did it (implementation code varies slightly from the details given in the question):

    module Authorisable
    def self.included(base)
    base.class_eval do
    base.extend ClassMethods
    end
    end

    module ClassMethods
      class PermissionAssociationBuilder
        def build_permissions_associations(klass)
          chains = build_chains_from(klass)
          chains.select! {|c| c.last.klass.included_modules.include? DistributedAuthorisation::Permissible}
          permissions_associations = []
          chains.each do |chain|
            source_name = :permissions
            chain.reverse.each do |r|
              assoc_name = :"#{r.name}_#{source_name}"
              r.active_record.has_many assoc_name, through: r.name.to_sym, source: source_name, class_name: DistributedAuthorisation::Permission
              source_name = assoc_name
            end
            permissions_associations << source_name
          end
          return permissions_associations
        end
    
        private
    
        def build_chains_from(klass)
          chains = reflections_to_follow(klass).map {|r| [r]}
          chains.each do |chain|
            models = chain.map {|r| r.klass}.unshift klass
            reflections_to_follow(models.last).each do |r|
              chains << (chain.clone << r) unless models.include? r.klass
            end
          end
        end
    
        def reflections_to_follow(klass)
          refs = klass.reflect_on_all_associations
          refs.reject {|r| r.options[:polymorphic] or r.is_a? ActiveRecord::Reflection::ThroughReflection}
        end
      end
    
      def permissions_associations
        @permissions_associations ||= PermissionAssociationBuilder.new.build_permissions_associations(self)
      end
    end
    

    Probably not the most efficient method, but it adds the chains I’m after with Klass.permissions_associations, and stores their symbols in class instance variable.

    I’d be happy to hear suggestions on how to improve it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.