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 4172428
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:33:44+00:00 2026-05-21T00:33:44+00:00

i have the following: class ThirdParty < ActiveRecord::Base # Dynamically adds accessors of the

  • 0

i have the following:

class ThirdParty < ActiveRecord::Base
  # Dynamically adds accessors of the requested kind.
  def self.has_many_children_of_kind (kinds=[])
    puts 'In generator method'
    kinds.each  { |k|
      n            = k.to_s
      self.class_eval  %{
        has_many    :#{n}_as_owner, {
                      :foreign_key  => :owner_id,
                      :class_name    => 'ThirdPartiesLink',
                      :conditions    => #{ k!=:third_parties ? "{ :kind => '"+n.singularize+"' }" : 'nil' }
                    }
        has_many    :#{n}, {
                      :through      => :#{n}_as_owner,
                      :source        => :owned
                    }
        }
      }
  end
  # Make dynamic associations of given kinds.
  has_many_children_of_kind  [ :third_parties, :customers, :suppliers, :contacts ]
end
class ThirdPartiesLink < ActiveRecord::Base
  belongs_to  :owner,  :foreign_key => :owner_id,  :class_name => 'ThirdParty'
  belongs_to  :owned,  :foreign_key => :owned_id,  :class_name => 'ThirdParty'
  # This model has a column named 'kind' for storing the link kind.
end

Everything is working exactly as i expect.
The line:

has_many_children_of_kind [ :third_parties, :customers, :suppliers, :contacts ]

Generates:

has_many :third_parties_as_owner, { :foreign_key => :owner_id, :class_name => 'ThirdPartiesLink', :conditions => nil }
has_many :third_parties, { :through => :third_parties_as_owner, :source => :owned } 
has_many :customers_as_owner, { :foreign_key => :owner_id, :class_name => 'ThirdPartiesLink', :conditions => { :kind => 'customer' } }
has_many :customers, { :through => :customers_as_owner, :source => :owned } 
has_many :suppliers_as_owner, { :foreign_key => :owner_id, :class_name => 'ThirdPartiesLink', :conditions => { :kind => 'supplier' } }
has_many :suppliers, { :through => :suppliers_as_owner, :source => :owned } 
has_many :contacts_as_owner, { :foreign_key => :owner_id, :class_name => 'ThirdPartiesLink', :conditions => { :kind => 'contact' } }
has_many :contacts, { :through => :contacts_as_owner, :source => :owned }

However, each time i refresh a page in which the ThirdParty object is used, the line ‘In generator method’ gets outputed in the console.

i have tried several things:
puting the has_many_children_of_kind in my application initializers instead of putting it in the ThirdParty class (i really don’t like that, it was more of a test than anything else). In this case, the first display of a page works after the server is restarted, but then if i refresh the page, the generated methods are not found when called on a ThirdParty instance…

What would be the way to make sure the ThirdParty class gets written with the accessors once for all at startup of the server?

Thanks for your time!
Pierre.

Edit: The generator method block could also be like this:

kinds.each  { |k|
  n            = k.to_s
 has_many("#{n}_as_owner".to_sym, {
      :foreign_key  => :owner_id,
      :class_name   => 'ThirdPartiesLink',
      :conditions   => ( k!=:third_parties ? { :kind => n.singularize } : nil)
    }
  )
 has_many(n.to_sym, {
      :through      => "#{n}_as_owner".to_sym,
      :source       => :owned
    }
  )
}

What is the best? The eval or the latest? i’d say the latest, because no parser / eval is involved, so it’s probably a bit faster, right?

  • 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-21T00:33:44+00:00Added an answer on May 21, 2026 at 12:33 am

    A good answer is the one pointed out by MikeOnRails.

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

Sidebar

Related Questions

I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)
I have the following: class Test @@a = 10 def show_a() puts a: #{@@a}
I have the following class: <?php /* * Abstract class that, when subclassed, allows
I have the following class which I'm fighting over how to implement. The question
I have the following class: public class ErrorMessage { public enum Severity { Error,
I have Java class that have to fetch the content of an URL online
I have acquired a DLL that was created in Visual Basic from a third
I have a BaseClass, a DerivedClass1 and a DerivedClass2 from a third party library.
Findbugs reports this: findbugs: [findbugs] Executing findbugs from ant task [findbugs] Running FindBugs... [findbugs]
firstly, let me quote a bit an essay from Expert Python Programming book: In

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.