Hi all

I've been playing with DataMapper in Merb and Sinatra and I've got a
question... Is there any equivalent to the ActiveRecord's after_add
hook? I want to update a property on an object when something is added
to its "has, n" collection.

Slightly simplified models:

class Story
  include DataMapper::Resource

  property :id,         Serial
  property :finished,   Boolean,    :default => false

  has 1..4, :parts
end

class Part
  include DataMapper::Resource

  property :id,         Serial
  belongs_to :story
end

When a Story has 4 Parts it is finished and I want to mark it as such.
I've tried adding after :save to Story but that doesn't seem to get
called when a Part is added. I've tried adding after :save to Part, to
check whether it is the 4th Part of its parent Story but while that
gets called, it's been problematic. Here's what I've tried in the Part
model:

after :save do
  if story.parts.count == 4
    story.finished = true
    story.save
  end
end

Doesn't seem to change the story.

after :save do
  if story.parts.count == 4
    s = Story.get(story.id)
    s.finished = true
    s.save
  end
end

Produces a "stack level too deep" trace.

after :save do
  if story.parts.count == 4
    story.attribute_set(:finished, true)
  end
end

Doesn't seem to change the story.

What am I doing wrong? Or is there another way around?

Any help much appreciated...

Mike
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to