Many thanks Yehuda.
Still no joy, I'm afraid. I thought maybe I was missing something
obvious. I'll tinker some more and see what happens.
In case you're interested, here's the hook and the stack trace:
after :save do
if story.parts.count == 4
story.finished = true
story.save!
end
end
http://pastie.org/399084
Cheers
Mike
On Feb 24, 9:00 pm, Yehuda Katz <[email protected]> wrote:
> You might want to try save! (save without validations) in your after save
> hook.
>
> -- Yehuda
>
> 2009/2/24 Mike Stenhouse <[email protected]>
>
>
>
>
>
> > 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
>
> --
> Yehuda Katz
> Developer | Engine Yard
> (ph) 718.877.1325
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---