
Simpler ways of defining associations
Reported by smoofles | July 28th, 2008 @ 05:29 PM
Let's assume there are an Article model and an Image model. The Article model would need a way to alias an Image in a one_to_one relation as a "Thumbnail".
Right now, as per dkubb on #datamapper, one has to write it like this:
Article.belongs_to :thumbnail, :class_name => 'Image', :child_key => [ :thumbnail_id ]
This, to my feeble mind ;o), makes little sense (hard to figure out), since my thinking is that the Article has One Thumbnail that is actually an Image – instead of the Article belonging to a Thumbnail. A syntax suggested by dkubb, which I think would be easier to understand, would be
Article.has 1, :thumbnail, :via => :image
or an alternative
Article.has 1, :thumbnail, :is => :image
Which would just define a "thumbnail" field in the Articles table and place a reference to the correct Image to use when an Image is assigned as an Article.thumbnail.
This is something that took me some time to get right, since I was "thinking wrong" - in terms of "has" instead of "belongs to".
Thanks for listening ;o)
smoofles
Comments and changes to this ticket
-
Dan Kubb (dkubb) December 1st, 2008 @ 02:05 AM
- State changed from new to invalid
- Assigned user cleared.
I think for now we're not going to change the association syntax. We want to keep it so the has() method is used in the parent, and implies ownership as in a parent/child relationship. I think it would be too confusing to change the meaning. Also the :via convention has been used by some of the dm-more plugins.
However, there may be a change to how belongs_to associations are defined. So in your Article class instead of saying:
belongs_to :thumbnail, :class_name => 'Image',
You'd say:
property :thumbnail, Image
So basically you'd be able to abstract what the "Image" is from the Article class. It could be a parent class, or a data type, or something else altogether (like an Embedded Value) and defined the same way.
For now I'm marking this as invalid.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
People watching this ticket
- Nobody is watching this ticket.