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

  • SEARCH
  • Home
  • 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 6342115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:10:35+00:00 2026-05-24T20:10:35+00:00

I understand why ActiveRecord can’t support has_many :through on polymorphic classes. But I would

  • 0

I understand why ActiveRecord can’t support has_many :through on polymorphic classes. But I would like to emulate some of its functionality. Consider the following, where a join table associates two polymorphic classes:

class HostPest < ActiveRecord::Base
  belongs_to :host, :polymorphic => true
  belongs_to :pest, :polymorphic => true
end
class Host < ActiveRecord::Base
  self.abstract_class = true  
  has_many :host_pests, :as => :host
end
class Pest < ActiveRecord::Base
  self.abstract_class = true  
  has_one :host_pest, :as => :pest
end
class Dog < Host ; end
class Cat < Host ; end
class Flea < Pest ; end
class Tick < Pest ; end

The goal

Since I can’t do has_many :pests, :through=>:host_pests, :as=>:host (etc), I’d like to emulate these four methods:

dog.pests (returns a list of pests associated with this dog)
flea.host (return the host associated with this flea)
cat.pests << Tick.create (creates a HostPest record)
tick.host = Cat.create (creates a HostPest record)

Question 1

I’ve got a working implementation for the first two methods (pests and host), but want to know if this is the best way (specifically, am I overlooking something in ActiveRecord associations that would help):

class Host < ActiveRecord::Base
  def pests
    HostPest.where(:host_id => self.id, :host_type => self.class).map {|hp| hp.pest}
  end
end
class Pest < ActiveRecord::Base
  def host
    HostPest.where(:pest_id => self.id, :pest_type => self.class).first.host
  end
end

Question 2

I’m stumped on how to implement the << and = methods implied here:

cat.pests << Tick.create  # => HostPest(:host=>cat, :pest=>tick).create
tick.host = Cat.create    # => HostPest(:host=>cat, :pest=>tick).create

Any suggestions? (And again, can ActiveRecord associations provide any help?)

  • 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-05-24T20:10:35+00:00Added an answer on May 24, 2026 at 8:10 pm

    Implementing the host= method on the Pest class is straight forward. We need to make sure we clear the old host while setting a new host (as AR doesn’t clear the old value from the intermediary table.).

    class Pest < ActiveRecord::Base
      self.abstract_class = true  
      has_one :host_pest, :as => :pest
    
      def host=(host)
        Pest.transaction do
          host_pest.try(:destroy) # destroy the current setting if any
          create_host_pest(:host => host)
        end
      end
    end
    

    Implementing pests<< method on Host class is bit more involved. Add the pests method on the Host class to return the aggregated list of pests. Add the << method on the object returned by pests method.

    class Host < ActiveRecord::Base
      self.abstract_class = true  
      has_many :host_pests, :as => :host
    
      # pest list accessor
      def pests
        @pests ||= begin
          host = self # variable to hold the current self. 
                      # We need it later in the block
          list = pest_list
          # declare << method on the pests list
          list.singleton_class.send(:define_method, "<<") do |pest|
            # host variable accessible in the block 
            host.host_pests.create(:pest => pest)
          end
          list
        end        
      end
    
    private
      def pest_list
        # put your pest concatenation code here
      end
    end
    

    Now

    cat.pests # returns a list
    cat.pests << flea # appends the flea to the pest list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand what System.WeakReference does, but what I can't seem to grasp is a
I'm using MySQL ActiveRecord with CodeIgniter to submit queries, but have run into some
I am running into some Rails 2.3.5 ActiveRecord behavior I do not understand. It
Trying to understand the effects of :has_many as its introduced in the Agile Web
I would like to limit the number of model Objects a user can create.
I understand how I can change the dns settings for my domains by editing
I understand that some countries have laws regarding website accessibility. In general, what are
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
I understand that they are both supposed to be small, but what are the
developers! I can't understand next situation For Example I have model class Pg::City <

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.