# Validates\_associated problem

**URL:** <https://www.ruby-forum.com/t/validates-associated-problem/54693>\
**Category:** Rails\
**Created:** [March 13, 2006, 9:21am UTC](https://www.ruby-forum.com/t/validates-associated-problem/54693 "2006-03-13T09:21:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sreechand\_boppudi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@sreechand\_boppudi](https://www.ruby-forum.com/u/sreechand_boppudi)\
**Post date:** [March 13, 2006, 9:21am UTC](https://www.ruby-forum.com/t/validates-associated-problem/54693/1 "2006-03-13T09:21:13Z")

</div>

Hello,  
I have a situation where an ‘employee’ belongs to a ‘department’ and  
have setup the relationship as follows.

class Employee \< ActiveRecord::Base  
belongs\_to :department, :foregin\_key =\> “department\_id”  
validates\_associated :department\_id  
end

class Department \< ActiveRecord::Base  
has\_many :employees

def validate\_on\_update  
unless self.find\_by\_id(id) error.add(:id, “is invalid department”)  
end

end

However when I try to save an employee with some test department ‘578’,  
get the following error because of validates\_associated in employee

“undefined method `valid?’ for “578”:String”

Any ideas what could be causing this?

Thanks

- Sree

---

<div class="post-metadata">

**Author:** ![sreechand\_boppudi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@sreechand\_boppudi](https://www.ruby-forum.com/u/sreechand_boppudi)\
**Post date:** [March 13, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/validates-associated-problem/54693/2 "2006-03-13T13:31:59Z")

</div>

Am Montag, den 13.03.2006, 09:21 +0100 schrieb sreechand boppudi:

> has\_many :employees  
> “undefined method `valid?’ for “578”:String”  
> You have an error in your Employee class. You must provide a symbol  
> referencing an association to the validates\_associated macro:

class Employee \< ActiveRecord::Base  
belongs\_to :department, :foregin\_key =\> “department\_id”  
validates\_associated :department # not :department\_id  
end

Additional i don’t understand your validate\_on\_update code: You are  
testing if the specific object exists in the database only (on\_update)  
if the object already exists in the database…

–  
Norman T.

[http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![sreechand\_boppudi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@sreechand\_boppudi](https://www.ruby-forum.com/u/sreechand_boppudi)\
**Post date:** [March 13, 2006, 5:25pm UTC](https://www.ruby-forum.com/t/validates-associated-problem/54693/3 "2006-03-13T17:25:38Z")

</div>

Norman,  
Thanks a lot for the information. My update validation code was just  
stub to test.  
Thanks again.

- Sree

Norman T. wrote:

> Am Montag, den 13.03.2006, 09:21 +0100 schrieb sreechand boppudi:
> 
> > has\_many :employees  
> > “undefined method `valid?’ for “578”:String”  
> > You have an error in your Employee class. You must provide a symbol  
> > referencing an association to the validates\_associated macro:
> 
> class Employee \< ActiveRecord::Base  
> belongs\_to :department, :foregin\_key =\> “department\_id”  
> validates\_associated :department # not :department\_id  
> end
> 
> Additional i don’t understand your validate\_on\_update code: You are  
> testing if the specific object exists in the database only (on\_update)  
> if the object already exists in the database…
> 
> –  
> Norman T.
> 
> [http://blog.inlet-media.de](http://blog.inlet-media.de)

---

<div class="post-metadata">

**Author:** ![sreechand\_boppudi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@sreechand\_boppudi](https://www.ruby-forum.com/u/sreechand_boppudi)\
**Post date:** [March 13, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/validates-associated-problem/54693/4 "2006-03-13T17:32:55Z")

</div>

I meant to say “validates\_associated :department”, or have the  
departmetn  
say something like “validates\_associated :person” or whatever the  
object is  
called.
