Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Association related validations seem to break on discriminator fields #16

Open
solnic opened this issue May 17, 2011 · 3 comments
Open
Milestone

Comments

@solnic
Copy link
Contributor

solnic commented May 17, 2011

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

@emmanuel
Copy link
Member

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.

@costi
Copy link

costi commented Nov 7, 2011

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

@solnic
Copy link
Contributor Author

solnic commented Nov 10, 2011

@costi can you reproduce it with DM from master?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants