Validates_associated problem


#1

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

#2

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


#3

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


#4

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