The best way to validate the presence of a belongs_to association?

63 views
Skip to first unread message

robs

unread,
Jun 30, 2009, 9:34:55 PM6/30/09
to DataMapper
For example:

class Person
include DataMapper::Resource

property :id, Integer, :serial => true
property :first_name, String, :nullable => false
property :last_name, String

has n :posts
end

class Post
include DataMapper::Resource

property :id, Integer, :serial => true
property :title, String, :nullable => false
property :body, Text

belongs_to :author, :class_name => "Person"
end

What would be the best way to validate the presence of an author when
a post is saved? I noticed that the documentation says that some
validations are automatically inferred (e.g., if :nullable => false
or :length is specified). Is there any way to include this validation
in the belongs_to method call? If not, what would anyone suggest?

Thanks!

Chris Van Pelt

unread,
Jun 30, 2009, 10:51:23 PM6/30/09
to datam...@googlegroups.com
validates_present :author

robs

unread,
Jun 30, 2009, 10:55:14 PM6/30/09
to DataMapper
That was embarrassingly easy. Thank you!

Chris Van Pelt

unread,
Jun 30, 2009, 10:57:16 PM6/30/09
to datam...@googlegroups.com
No problem :)
Reply all
Reply to author
Forward
0 new messages