#1150 ✓not-applicable
Matt Royal

dm-timestamps should set timestamps before validation when dm-validations is used

Reported by Matt Royal | December 11th, 2009 @ 12:55 AM

In Datamapper 0.10.1, if both dm-validations and dm-timestamps, then creating a new record will fail unless you explicitly set updated_at and created_at. This happens because dm-timestamps sets the timestamps using a before(:save) hook, but dm-valiations enforces validation before this hook is run.

Here's an example...

class Post
  include DataMapper::Resource

  property :id, Serial
  property :title, String, :nullable => false
  property :body, Text, :nullable => false
  property :created_at, DateTime, :nullable => false
  property :updated_at, DateTime, :nullable => false
end

p = Post.new(:title => 'abc', :body => 'def')
p.valid? # => false
p.errors.full_messages # => ["Created at must not be blank", "Updated at must not be blank"]

Comments and changes to this ticket

  • kristian

    kristian January 31st, 2010 @ 08:04 AM

    • State changed from “new” to “unconfirmed”

    instead of

    property :created_at, DateTime, :nullable => false
    property :updated_at, DateTime, :nullable => false
    
    either use
    timestamps :at
    
    or
    property :created_at, DateTime, :nullable => false, :auto_validation => false
    property :updated_at, DateTime, :nullable => false, :auto_validation => false
    
    or see readme of dm-timestamps for more working solutions
  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) May 26th, 2010 @ 02:20 AM

    • State changed from “unconfirmed” to “not-applicable”

    I am marking this as not-applicable because you should be either using the helper methods in dm-timestamps to add these properties, or specifying the properties similarly to how the helper methods work.

    Having timestamps set during validation isn't valid behaviour anyway. The idea is to set the timestamps immediately when the record is saved. If it's done during validation, and the record is invalid, then the timestamps are set on an unsaved resource. When/if the resource is saved, then those values may not be correct anymore.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages