There is no validation in the Parent class. Which means a parent
records must have an id but the name can be nil. Whereas in the child
record, the parent id cannot be nil.

In the Parent class you have to make

property :name, String, :required =>  true

to achieve what you want

On Thu, Jun 27, 2013 at 9:16 PM, Neil C <n...@vidyasource.com> wrote:
> Imagine two classes:
>
> class Parent
>   include DataMapper::Resource
>
>   property :id, Serial
>   property :name, String
>   ...
>
> class Child
>   include DataMapper::Resource
>
>   property :id, Serial
>   property :position, Integer, :required => true, :default => -1
>
>   belongs_to :parent, :key => true
>
> When I do child.parent.name, I sometimes get an error indicating parent is
> nil. How is that possible when belongs_to defaults to required?
>
> Please let me know how I can set up my associations so that this nil parent
> can never happen.
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google Groups
> "DataMapper" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to datamapper+unsubscr...@googlegroups.com.
> To post to this group, send email to datamapper@googlegroups.com.
> Visit this group at http://groups.google.com/group/datamapper.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to datamapper+unsubscr...@googlegroups.com.
To post to this group, send email to datamapper@googlegroups.com.
Visit this group at http://groups.google.com/group/datamapper.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to