# ActiveRecord (not necessarily rails)

**URL:** <https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958>\
**Category:** Ruby\
**Created:** [June 27, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958 "2007-06-27T21:05:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 27, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/1 "2007-06-27T21:05:45Z")

</div>

Hello all,  
i have been using ActiveRecord in a couple of GUI applications recently  
and  
it is working really well.  
i was wondering though, if it is possible to have a class defined that i  
can  
change one of the has\_many or belongs\_to declarations  
dynamically. In a model, i declare one like this.

class Person ActiveRecord::Base  
has\_many :records

now, is the has\_many some sort of attribute, class variable, etc…  
i mean if in my program, i wanted to change the  
has\_many :records to something like  
has\_many :hats

how could i do that?

thanks for any tips  
shawn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 27, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/2 "2007-06-27T22:20:54Z")

</div>

Hi,

On Jun 27, 2007, at 4:05 PM, shawn bright wrote:

> has\_many :records
> 
> now, is the has\_many some sort of attribute, class variable, etc…  
> i mean if in my program, i wanted to change the  
> has\_many :records to something like  
> has\_many :hats

`
has_many :hats, :class_name => 'Record', :foreign_key => 'hat_id'
`

Good luck!

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 27, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/3 "2007-06-27T22:26:54Z")

</div>

no, i am sorry, i was not clear enough.  
is there a way, programatically, that i change the behavior of a model  
while  
still using the model.  
is there a way to make it change behavior from has\_many :records to  
has\_many  
:hats on the fly.  
like is there a variable i can pass to an active record object to make  
it  
change behavior ?

thanks  
sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 27, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/4 "2007-06-27T22:32:54Z")

</div>

> no, i am sorry, i was not clear enough.  
> is there a way, programatically, that i change the behavior  
> of a model while still using the model.  
> is there a way to make it change behavior from has\_many  
> :records to has\_many :hats on the fly.

You could probably do that using class\_eval, but it wouldn’t last past  
the end of the session.

- donald

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 27, 2007, 11:30pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/5 "2007-06-27T23:30:00Z")

</div>

> ok, i will look that up. I am not looking to build a rails  
> app with this, i want to build a GUI app where our customers  
> can point different attributes to different models ( kinda  
> like setting a foriegn key dynamically, we are dealing with  
> lots of models here. So, i suppose i could store them in a  
> configuration file or something.

I suspect you’d be much happier if these related attributes inherited  
from the same supermodel and used single-table inheritance, or if you  
used polymorphic associations instead. The latter sounds like it’s more  
appropriate to your situation, but if your attribute models have similar  
characteristics, the former would be a better fit.

Either sound more reasonable than mucking about with monkey-patching the  
class at run-time. Not nearly as much fun, mind you, but that’s not  
always the best thing for which to optimize.

- donald

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 27, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/6 "2007-06-27T23:00:58Z")

</div>

ok, i will look that up. I am not looking to build a rails app with  
this, i  
want to build a GUI app where our customers can point different  
attributes  
to different models ( kinda like setting a foriegn key dynamically, we  
are  
dealing with lots of models here. So, i suppose i could store them in a  
configuration file or something.

thanks for your help

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 28, 2007, 12:33am UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/7 "2007-06-28T00:33:06Z")

</div>

Hi –

On Thu, 28 Jun 2007, Giles B. wrote:

> > used polymorphic associations instead. The latter sounds like it’s more  
> > appropriate to your situation, but if your attribute models have similar  
> > characteristics, the former would be a better fit.
> > 
> > Either sound more reasonable than mucking about with monkey-patching the  
> > class at run-time. Not nearly as much fun, mind you, but that’s not  
> > always the best thing for which to optimize.
> 
> You can do that:
> 
> Foo.class\_eval(“has\_many :bars”)

I think you could just do:

Foo.has\_many :bars

However…

> But I think it could be an utterly heinous mess. You’d need the DB to  
> morph in an equivalently dynamic way, which is nontrivial.

…I totally agree about the heinous mess 🙂

David

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 28, 2007, 4:32am UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/8 "2007-06-28T04:32:22Z")

</div>

ok, gents, your comments are well heeded. I am still a newbie here and a  
mess is not what i want to try to fix later when i am already in  
production  
with this and want to fix it.

thanks, will keep looking

shawn

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 28, 2007, 12:23am UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/9 "2007-06-28T00:23:05Z")

</div>

On 6/27/07, Ball, Donald A Jr (Library) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> appropriate to your situation, but if your attribute models have similar  
> characteristics, the former would be a better fit.
> 
> Either sound more reasonable than mucking about with monkey-patching the  
> class at run-time. Not nearly as much fun, mind you, but that’s not  
> always the best thing for which to optimize.

You can do that:

Foo.class\_eval(“has\_many :bars”)

But I think it could be an utterly heinous mess. You’d need the DB to  
morph in an equivalently dynamic way, which is nontrivial.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 28, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/10 "2007-06-28T18:59:57Z")

</div>

On 6/27/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi –
> 
> On Thu, 28 Jun 2007, Giles B. wrote:

> > Foo.class\_eval(“has\_many :bars”)
> 
> I think you could just do:
> 
> Foo.has\_many :bars

And even if you couldn’t,  
Foo.send(:has\_many,:bars) or  
Foo.class\_eval { has\_many :bars } would be a whole lot safer.

Giles, are you just using eval(string) for the fun of it now? 🙂

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 28, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/11 "2007-06-28T21:44:34Z")

</div>

> > > Foo.class\_eval(“has\_many :bars”)
> > 
> > I think you could just do:
> > 
> > Foo.has\_many :bars
> 
> And even if you couldn’t,  
> Foo.send(:has\_many,:bars) or  
> Foo.class\_eval { has\_many :bars } would be a whole lot safer.

“Foo.has\_many” works. Just tested it.

> Giles, are you just using eval(string) for the fun of it now? 🙂

I’m on a slippery slope. eval() is its own gateway drug.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [July 1, 2007, 5:15am UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/12 "2007-07-01T05:15:42Z")

</div>

On 6/27/07, Giles B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 6/27/07, Ball, Donald A Jr (Library) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I suspect you’d be much happier if these related attributes inherited  
> > from the same supermodel and used single-table inheritance, or if you  
> > used polymorphic associations instead. The latter sounds like it’s more  
> > appropriate to your situation, but if your attribute models have similar  
> > characteristics, the former would be a better fit.

> You can do that:
> 
> Foo.class\_eval(“has\_many :bars”)
> 
> But I think it could be an utterly heinous mess. You’d need the DB to  
> morph in an equivalently dynamic way, which is nontrivial.

To shawn: I haven’t used ActiveRecord yet, but there are probably ways  
of changing the database schema ‘on the fly’, as it were. Just be  
careful, is all.

Ruby will take care of semantics and leave the database in tact if  
that’s all you are worried about, but you also then tread on dangerous  
waters.

You’re sort of asking for a dynamic model. Lot’s of overhead involved.

Todd

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [June 29, 2007, 12:09am UTC](https://www.ruby-forum.com/t/activerecord-not-necessarily-rails/107958/13 "2007-06-29T00:09:58Z")

</div>

On 6/27/07, shawn bright [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> now, is the has\_many some sort of attribute, class variable, etc…  
> has\_many is a class method.

> i mean if in my program, i wanted to change the  
> has\_many :records to something like  
> has\_many :hats
> 
> how could i do that?

This sounds like an “XY problem”  
([http://www.perlmonks.org/index.pl?node\_id=542341](http://www.perlmonks.org/index.pl?node_id=542341)). What are you  
trying to accomplish?
