ActiveRecord (not necessarily rails)

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

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!

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

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

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

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

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 :slight_smile:

David

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

On 6/27/07, Ball, Donald A Jr (Library) [email protected]
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
Portfolio: http://www.gilesgoatboy.org

On 6/27/07, [email protected] [email protected] 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? :slight_smile:

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? :slight_smile:

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


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org

On 6/27/07, Giles B. [email protected] wrote:

On 6/27/07, Ball, Donald A Jr (Library) [email protected] 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

On 6/27/07, shawn bright [email protected] 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). What are you
trying to accomplish?