[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: ActiveRecord (not necessarily rails

Ball, Donald A Jr (Library)

6/27/2007 9:29:00 PM

> 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

5 Answers

Giles Bowkett

6/27/2007 10:22:00 PM

0

On 6/27/07, Ball, Donald A Jr (Library) <donald.ball@nashville.gov> wrote:
> > 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.

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.bl...
Portfolio: http://www.gilesg...

dblack

6/27/2007 10:31:00 PM

0

Gregory Brown

6/28/2007 4:59:00 PM

0

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

Giles Bowkett

6/28/2007 7:43:00 PM

0

> > > 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.bl...
Portfolio: http://www.gilesg...

Todd Benson

7/1/2007 3:15:00 AM

0

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