# conditional model validation

**URL:** <https://discuss.rubyonrails.org/t/conditional-model-validation/40798>\
**Category:** rubyonrails-talk\
**Created:** [March 23, 2009, 8:26pm UTC](https://discuss.rubyonrails.org/t/conditional-model-validation/40798 "2009-03-23T20:26:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzimmek](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@jzimmek](https://discuss.rubyonrails.org/u/jzimmek)\
**Post date:** [March 23, 2009, 8:26pm UTC](https://discuss.rubyonrails.org/t/conditional-model-validation/40798/1 "2009-03-23T20:26:20Z")

</div>

i really like the declarative model-based validation in rails, but i run into trouble while doing some real world (more complex) stuff.

class User \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :address &nbsp;&nbsp;accepts\_nested\_attributes\_for :address &nbsp;&nbsp;def needs\_street\_validation? &nbsp;&nbsp;&nbsp;&nbsp;true &nbsp;&nbsp;end end

class Address \< ActiveRecord::Base &nbsp;&nbsp;has\_one :user &nbsp;&nbsp;validates\_presence\_of :street, :if =\> Proc.new { |adr| adr.user.needs\_street\_validation? } end

u = User.new u.address = Address.new u.save

NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.needs\_street\_validation? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from app/models/address.rb:3

seems to me like we cannot reference any associated object within the ":if" condition or do you know some trick to get this working ?

maybe this is not the best example, but i hope you got the point.

in most more-complex gui's we have conditional validation based on the state of multiple objects. It would be a shame if we could only handle those validation programmatically.

tested in a vanilla rails 2.3.2 application.

any help would be really appreciated.

regards jan

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [March 23, 2009, 9:18pm UTC](https://discuss.rubyonrails.org/t/conditional-model-validation/40798/2 "2009-03-23T21:18:01Z")

</div>

> u = User.new u.address = Address.new u.save
> 
> NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.needs\_street\_validation? from app/models/address.rb:3
> 
> seems to me like we cannot reference any associated object within the ":if" condition or do you know some trick to get this working ?

The problem isn't anything to do with :if's, more a general quirk of activerecord - When you assign Address.new to u.address activerecord doesn't update the inverse assocation on the address object. In the case where you're doing this with already saved objects you can use tricks like set\_#{association\_name}\_target to do that work of filling in the inverse association, though I don't know if the fact that your objects are unsaved makes a difference.

Fred
