Topic: activeRecord method for getting a list of things the aR has_many of?

Is there such a method, or a relevant hack?

Re: activeRecord method for getting a list of things the aR has_many of?

Not sure I follow. If I do, then this is standard fair in RoR

class User < ActiveRecord::Base
   has_many :pigs
end

You can get to User's many pigs easily

u = User.find(1)
his_pigs = u.pigs

Last edited by tortoise (2006-12-18 18:14:10)

Re: activeRecord method for getting a list of things the aR has_many of?

I'm sorry, I should have been clearer: I'd like something like this:

def dream_method(some_active_record)
    collection c = some_active_record.get_a_collection_of_things_this_record_has_many_of
end

To extend your example, dream_method(User) would return ['Pigs'].

In other words, I want to call the method without knowing what Model I'm dealing with, or what (or how many) I'm going to get back. I want this so that I can write reusable partials that can display complex models for editing.

What I'm thinking right now is that I'll have to extend ActiveRecord to maintain a list of has_whatever associations as they're added, but I feel like there must be an easier answer.