How are you guys getting around the lack of polymorphic associations?

324 views
Skip to first unread message

Bryan

unread,
Mar 16, 2009, 3:50:34 AM3/16/09
to DataMapper
Apart from dm-is-remixable. I kinda like having ONE 'comments' table
rather than 5. I've looked at serveral solutions but none of them seem
to work (outdated maybe).

I've hit a jam with a project of mine and I'm out of ideas. So I'm
wondering what you guys are doing to get around this.

Thanks,
Bryan

Michael Klishin

unread,
Mar 16, 2009, 4:48:29 AM3/16/09
to datam...@googlegroups.com

On 16.03.2009, at 10:50, Bryan wrote:

> Apart from dm-is-remixable. I kinda like having ONE 'comments' table
> rather than 5. I've looked at serveral solutions but none of them seem
> to work (outdated maybe).
>

I used dm-is-remixable and plain Ruby modules, and happy to not even
bother with polymorphic
associations in the first place. Polymorphic
associations are cocain of ORM world, "kinda nice" and "cool kids use
them" and "caused you no harm yet" until
it is too late.

Having just one table
instead of N is not that useful from practical point of view.

I personally only find object level polymorphism important, and how to
share bits of behavior with modules, and not
what tables those objects go to sleep to. After all, the whole point
of DM is to hide storage details from you.

http://gist.github.com/cef3c9bf2f756779aca2

This is callback methods idiom. It is useful in many simple cases with
lazy module evaluation trick.
Here callback methods approach won't work since you need to specify
option for each macro call.

class Widget
is :state_machine, :field => :activation
is :state_machine, :field => :face
end

Macros have only
one advantage: if you have multiple associations of the same kind, you
need options for each call and not one set of callback methods
per class.

Hope this helps.

MK

Tze Yang Ng

unread,
Mar 18, 2009, 1:26:06 AM3/18/09
to datam...@googlegroups.com
Hi Michael,

Saw the gist u have posted ... very cool use of "class Xyz < Module".
Took me quite sometime to figure out what it actually does.

Cheers

==
--
http://ngty77.blogspot.com

Michael Klishin

unread,
Mar 18, 2009, 6:08:23 AM3/18/09
to datam...@googlegroups.com

On 18.03.2009, at 8:26, Tze Yang Ng wrote:

> Saw the gist u have posted ... very cool use of "class Xyz < Module".

Delays Module body evaluation so one can use validations, has/
belongs_to and
other class methods in it without having two inner modules.

> Took me quite sometime to figure out what it actually does.


Probably not so cool then ;)

MK

Reply all
Reply to author
Forward
0 new messages