Forum: Ruby ActiveRecord (not necessarily rails)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-06-27 21:05
(Received via mailing list)
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
6f8347a5bb706beb3205635958589caf?d=identicon&s=25 Igor Sutton Lopes (Guest)
on 2007-06-27 22:20
(Received via mailing list)
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

<code>
has_many :hats, :class_name => 'Record', :foreign_key => 'hat_id'
</code>

Good luck!
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-06-27 22:26
(Received via mailing list)
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
E1f43bafda26307a050d11902752b2a6?d=identicon&s=25 Ball, Donald A Jr (Library) (Guest)
on 2007-06-27 22:32
(Received via mailing list)
> 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
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-06-27 23:00
(Received via mailing list)
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
E1f43bafda26307a050d11902752b2a6?d=identicon&s=25 Ball, Donald A Jr (Library) (Guest)
on 2007-06-27 23:30
(Received via mailing list)
> 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
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2007-06-28 00:23
(Received via mailing list)
On 6/27/07, Ball, Donald A Jr (Library) <donald.ball@nashville.gov>
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 Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-28 00:33
(Received via mailing list)
Hi --

On Thu, 28 Jun 2007, Giles Bowkett 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
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2007-06-28 04:32
(Received via mailing list)
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
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2007-06-28 18:59
(Received via mailing list)
On 6/27/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Thu, 28 Jun 2007, Giles Bowkett 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? :)
Ce8b03e5750097942c58e12b46724312?d=identicon&s=25 Giles Bowkett (Guest)
on 2007-06-28 21:44
(Received via mailing list)
> > > 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 Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
813f535246722b7bf02aacc9ce818de8?d=identicon&s=25 Bob Showalter (Guest)
on 2007-06-29 00:09
(Received via mailing list)
On 6/27/07, shawn bright <nephish@gmail.com> 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?
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-07-01 05:15
(Received via mailing list)
On 6/27/07, Giles Bowkett <gilesb@gmail.com> wrote:
> On 6/27/07, Ball, Donald A Jr (Library) <donald.ball@nashville.gov> 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
This topic is locked and can not be replied to.