i have the problem that my child class fields aren''t validated on saving the parent. working with parent and child is fine. if all fields are filled correctly everything is saved as expected. but missing fields in the child arent added to the errors. following short sample shows what i mean: Model ----------------------------------------------------- class Parent < ActiveRecord::Base has_one :children validates_presence_of :parent_name def init! self.build_children end end class Children < ActiveRecord::Base belongs_to :parent validates_presence_of children_name end controller ------------------------------------------------------ @parent = session[:parent] ||= Parent.new @parent.init! @parent.save => the absence of parent_name will be correctly shown @parent.errors but i cant find the absence of children_name. isnt it validated? if parent_name is set and children name is nullable in DB everything will be saved without validating the children. i think im missing something important at this pont. any help? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Jan-24 21:24 UTC
Re: validates_presence_of in parent ok. but how in children?
dweinand wrote:> i have the problem that my child class fields aren''t validated on saving > the > parent. working with parent and child is fine. > if all fields are filled correctly everything is saved as expected. > but missing fields in the child arent added to the errors. > following short sample shows what i mean: > > Model > ----------------------------------------------------- > class Parent < ActiveRecord::Base > has_one :children > validates_presence_of :parent_name > > def init! > self.build_children > end > end > > class Children < ActiveRecord::Base > belongs_to :parent > validates_presence_of children_name > end > > controller > ------------------------------------------------------ > @parent = session[:parent] ||= Parent.new > @parent.init! > @parent.save > > => > the absence of parent_name will be correctly shown @parent.errors > but i cant find the absence of children_name. isnt it validated? > > if parent_name is set and children name is nullable in DB > everything will be saved without validating the children.You need to add validates_associated :children to your Parent class. (You''d get this automatically if it was a has_many rather than a has_one.) -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
dweinand
2007-Jan-24 23:05 UTC
Re: validates_presence_of in parent ok. but how in children?
yeah. thank you. i found validates_associated :children a little while after posting here. pretty simple if you know it. ;) thank you. On 24 Jan., 22:24, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> dweinand wrote: > > i have the problem that my child class fields aren''t validated on saving > > the > > parent. working with parent and child is fine. > > if all fields are filled correctly everything is saved as expected. > > but missing fields in the child arent added to the errors. > > following short sample shows what i mean: > > > Model > > ----------------------------------------------------- > > class Parent < ActiveRecord::Base > > has_one :children > > validates_presence_of :parent_name > > > def init! > > self.build_children > > end > > end > > > class Children < ActiveRecord::Base > > belongs_to :parent > > validates_presence_of children_name > > end > > > controller > > ------------------------------------------------------ > > @parent = session[:parent] ||= Parent.new > > @parent.init! > > @parent.save > > > => > > the absence of parent_name will be correctly shown @parent.errors > > but i cant find the absence of children_name. isnt it validated? > > > if parent_name is set and children name is nullable in DB > > everything will be saved without validating the children.You need to add > > validates_associated :children > > to your Parent class. > > (You''d get this automatically if it was a has_many rather than a has_one.) > > -- > We develop, watch us RoR, in numbers too big to ignore.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---