Forum: Ruby on Rails validates_associated problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7b14490089ddfd107279984a7e844d41?d=identicon&s=25 sreechand boppudi (Guest)
on 2006-03-13 09:21
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
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-13 13:31
(Received via mailing list)
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 Timmler

http://blog.inlet-media.de
7b14490089ddfd107279984a7e844d41?d=identicon&s=25 sreechand boppudi (Guest)
on 2006-03-13 17:25
Norman,
Thanks a lot for the information. My update validation code was just
stub to test.
Thanks again.
- Sree


Norman Timmler 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 Timmler
>
> http://blog.inlet-media.de
2c80e1b0837eec76d38d4f7fa1c15e87?d=identicon&s=25 Michael Gorsuch (Guest)
on 2006-03-13 17:32
(Received via mailing list)
I meant to say "validates_associated :department", or have the
departmetn
say something like "validates_associated :person"  or whatever the
object is
called.
This topic is locked and can not be replied to.