Dynamic associations

Hi,
This is my first post to this forum, glad to meet you all!

I have a question about dynamic associations.

Say I have an application that creates tables after installation (via a
user action). Say an extra table gets created that details a shop's
coffee purchases.

I've seen somewhere on the net you can just eval the code to create our
CoffeePurchases class but how do we dynamically add a "has_many"
declaration to the Shop class?

Hi,
This is my first post to this forum, glad to meet you all!

I have a question about dynamic associations.

Say I have an application that creates tables after installation
(via a
user action). Say an extra table gets created that details a shop's
coffee purchases.

I've seen somewhere on the net you can just eval the code to create
our
CoffeePurchases class but how do we dynamically add a "has_many"
declaration to the Shop class?

has_many is just a method call, so (in theory) at any time you want
you can call Shop.has_many :foos... (if has_many is a protected method
just use send).
Not sure this is a great idea though, as there are several
complications:
- if your app is restarted you'll have to repeat all this
- unless you want really weird behaviour you'll have to run your class
generating, method adding code on all of your mongrels/mod_rails
instances etc...

Fred

Frederick Cheung wrote:

- if your app is restarted you'll have to repeat all this
- unless you want really weird behaviour you'll have to run your class
generating, method adding code on all of your mongrels/mod_rails
instances etc...

Fred

Thanks!

Yeah, I know its a fairly wacky idea, i'm just looking at all the
options. On this note, I haven't figured out a way to traverse the
object tree. I would like to take an object instance, find its
associations and list them (possibly crawling through the child objects
as well). I know there must be a way to do it but I just can't find it
:frowning:

Frederick Cheung wrote:

- if your app is restarted you'll have to repeat all this
- unless you want really weird behaviour you'll have to run your
class
generating, method adding code on all of your mongrels/mod_rails
instances etc...

Fred

Thanks!

Yeah, I know its a fairly wacky idea, i'm just looking at all the
options. On this note, I haven't figured out a way to traverse the
object tree. I would like to take an object instance, find its
associations and list them (possibly crawling through the child
objects
as well). I know there must be a way to do it but I just can't find it

some_object.class.reflections (a hash, the keys are the association
names, the value is a object describing the association).
reflections also includes aggregations (composed_of), but you can
filter those out by ignoring anything that isn't a
ActiveRecord::Reflection::AssociationReflection

Fred

Frederick Cheung wrote:

Thanks!

Yeah, I know its a fairly wacky idea, i'm just looking at all the
options. On this note, I haven't figured out a way to traverse the
object tree. I would like to take an object instance, find its
associations and list them (possibly crawling through the child
objects
as well). I know there must be a way to do it but I just can't find it

some_object.class.reflections (a hash, the keys are the association
names, the value is a object describing the association).
reflections also includes aggregations (composed_of), but you can
filter those out by ignoring anything that isn't a
ActiveRecord::Reflection::AssociationReflection

Fred

Depending on your needs, you might also want to look at
object.class.reflect_on_all_associations. You can pass to that method
:belongs_to if you are only interested in the "parent" hierarchy. I just
used that method and some recursion to traverse up and out along the
belongs_to paths. At some point in the (hopefully) near future, I'm
going to post the code with an explanation.

Phillip