Open solnic opened 13 years ago
I'm pretty sure this is a consequence of Resource#valid?
only reflecting the validity of the receiver (the current resource), whereas Resource#save
is dependent on the validity of loaded related Resources (relationships set on the current resource).
In my opinion, the behavior of Resource#valid?
should be synchronized with Resource#save
(as implied by this issue), or a new method should be introduced to check the validity of the whole resource object graph (current resource and any loaded relationships), eg., Resource#all_valid?(context)
or the like.
I just encountered this problem myself. I got a some specs to prove the broken behaviour. First test passes, second one fails.
require 'rubygems'
require 'data_mapper'
DataMapper.setup(:default, 'sqlite::memory:')
class Address
include DataMapper::Resource
property :id, Serial
end
class Person
include DataMapper::Resource
property :id, Serial
belongs_to :address
end
class Man < Person
end
DataMapper.auto_migrate!
describe 'person' do
it 'requires address' do
person = Person.new
person.valid?
person.errors[:address_id].should include("Address must not be blank")
end
end
describe 'man' do
it 'requires address' do
man = Man.new
man.valid?
man.errors[:address_id].should include("Address must not be blank")
end
end
@costi can you reproduce it with DM from master?
http://gist.github.com/605956
I've posted a sample there. Notice how dog is valid without a human, but save fails.
Created by pathsny (at gmail) - 2010-10-01 09:26:45 UTC
Original Lighthouse ticket: http://datamapper.lighthouseapp.com/projects/20609/tickets/1424