Sunday, August 22, 2010

getting started with DataMapper

I've just spent all day trying to get started with DataMapper

I've found a few gotchas:

say i have two model objects with a many to many relationship,
given two instances of those:
t = Thing.create
w = Whatever.create

creating a joining resource:

ThingWhatever.create(:thing => t, :whatever => w)

[t] == w.things #true
[w] == t.whatevers #true

#then create a second join resource from t to a new Whatever

ThingWhatever.create(:thing => t, :whatever => (w2 = Whatever.create))

[w,w2] == t.whatevers #false

I'm new to DataMapper so this was very confusing.

eventaully i discovered I could work around the problem with:

[w,w2] == t.whatevers #true

however, reload is not mentioned in the getting started documentation and it took me hours to figure out why it wasn't working.

It's a bit of a leaky abstraction. you can see the database through the cracks.

posted a ticket

I considered trying ActiveRecord but it smelled even leakier.

1 comment:

  1. I would highly recommend checking out the IRC channel #datamapper on There's almost always someone there, and you can get help with problems like this.

    One of the problems with documenting ORMs is that the process of using the tool is not linear. We've documented most of the public methods in isolation using YARD, but that's not enough, you need to have tutorials that walk you through common scenarios. The problem is that there are hundreds of use cases, and you could write the hundreds of tutorials and still not cover all of them.. having too much documentation is just as bad as having too little. (although I'd rather have the problem of too much than too little).

    We are also working towards some more tutorials, because I will agree with you that they need more work.

    The approach we've been using is to add documentation for gotchas as people new to DM find them, and then examine the API to see if it can be cleaned up to make it more straight forward. The goal is that things should just work as you'd expect them with POLS in effect (POLS = Principle of Least Surprise).