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 -- Posted via http://www.ruby-forum.com/.
Am Montag, den 13.03.2006, 09:21 +0100 schrieb sreechand boppudi:> 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"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
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-- Posted via http://www.ruby-forum.com/.
I meant to say "validates_associated :department", or have the departmetn say something like "validates_associated :person" or whatever the object is called. On 3/13/06, sreechand boppudi <sreechand@hotmail.com> wrote:> > 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060313/b18fe956/attachment.html