Hi -

I've run into a bit of a surprise saving associations with DM
0.9.11...

Given two associated models  -

----
class Author
  include DataMapper::Resource
  property :id, Serial
  has n, :articles
end

class Article
  include DataMapper::Resource
  property :id, Serial
  belongs_to :author
end
-----

If I do the following -

-----
article = Article.new
article.author = Author.new  # add a parent
article.save   # save the child
-----

both the article and associated author objects will be saved. All
good. However, what surprised me is that if I add the following to the
Article model -

  validates_present :author_id, :message => "missing author"

saving the article will fail.

Looking at the DM code, I can see why this happens - the validation
check gets applied before attempting to save the child object and
therefore before any attempt to save the parent object, which
ordinarily would happen automatically when the child is saved and
which is needed to enable the foreign key to be added to the child.

This seems a little odd - the validation fails even though in the
absence of the validation the objects are saved exactly the way I
expect and completely satisfy the condition the validation was
intended to test for.

A bug or a feature? What's the best way to deal with this? I'm
currently just saving the parent explicitly, but it would be nice to
be able to just save the child.

Mark.
--~--~---------~--~----~------------~-------~--~----~
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