DataMapper: Create new record or update existing

Posted by John Topley on Stack Overflow See other posts from Stack Overflow or by John Topley
Published on 2010-04-02T16:08:07Z Indexed on 2010/04/11 14:33 UTC
Read the original article Hit count: 238

Filed under:
|

Does DataMapper provide a convenient way to create a new record when none exists or update an existing one? I couldn't find anything in the API documentation.

This is what I have at the moment which doesn't seem very elegant:

foo = Foo.get(id)
if foo.nil?
  foo = Foo.create(#attributes...)
else
  foo.update(#attributes...)
end
foo.save

© Stack Overflow or respective owner

Related posts about datamapper

Related posts about ruby