i am trying to validate a field depending on attributes from the associated model. looking at the code will make more sense (i hope)
class User < ActiveRecord::Base
has_and_belongs_to_many :user_groups
has_one :profile, :dependent => :destroy
accepts_nested_attributes_for :profile
validates_associated \
:profile,
:allow_destroy => true
end
class Profile < ActiveRecord::Base
belongs_to :user
validates_presence_of \
:business_name,
:if => self.user.user_groups.first.name == 'Client'
end
when i submit the form to create a new user, i get
undefined method `user_groups' for nil:NilClass
basically i only want to validate the presence of the field business_name if i am creating a new client.
i have also tried using
:if => Proc.new { |p| p.user.user_groups.first.name == 'Clients' }
with the same results.
maybe i am barking up entirely the wrong tree, but any suggestions on accomplishing this?