Mlle
2010-May-07 13:35 UTC
Reject a nested object if all parameters of its own nested object are blank
Hello, I have a model called Sample that has many Treatments. A Treatment can have many Results. I want to reject the creation of a Treatment if all its fields are blank and if all the fields of each of its nested Results are blank. I have this in the Sample model: accepts_nested_attributes_for :treatments, :allow_destroy => true, :reject_if => proc { |attributes| attributes[''treatmenttype''].blank? && attributes[''author''].blank? && attributes[''results_attributes''].blank? } This part: attributes[''results_attributes''].blank? only makes it reject the Treatment if there are no Results, but how can I write the conditions so that it will reject the creation of a Treatment if all the attirubtes of each Result are blank? Right now it creates the Treatment if the Result exists, regardless of whether all its fields are blank. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fattah
2010-May-07 14:46 UTC
RE: Reject a nested object if all parameters of its own nested object are blank
You can write method to validate nested result attributes. Def valid_nested_results?(results_attributes) results_attributes.each do|res_attr| return false if(res_attr[''attr1''].blank? and res_attr[''attr2''].blank? ....) end return true end And use this method in reject_if instead of attributes[''results_attributes''].blank? Or you can also define the appropriate :reject_if for nested attribute definition for results in treatment model. Does it help? -----Original Message----- From: Mlle <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> Sent: Friday, 7 May 2010 7:35 PM To: Ruby on Rails: Talk <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Subject: [Rails] Reject a nested object if all parameters of its own nested object are blank Hello, I have a model called Sample that has many Treatments. A Treatment can have many Results. I want to reject the creation of a Treatment if all its fields are blank and if all the fields of each of its nested Results are blank. I have this in the Sample model: accepts_nested_attributes_for :treatments, :allow_destroy => true, :reject_if => proc { |attributes| attributes[''treatmenttype''].blank? && attributes[''author''].blank? && attributes[''results_attributes''].blank? } This part: attributes[''results_attributes''].blank? only makes it reject the Treatment if there are no Results, but how can I write the conditions so that it will reject the creation of a Treatment if all the attirubtes of each Result are blank? Right now it creates the Treatment if the Result exists, regardless of whether all its fields are blank. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Mlle
2010-May-11 17:23 UTC
Re: Reject a nested object if all parameters of its own nested object are blank
oh, it works if you reject the creation of the third-level nested model in the second-level nested model. On May 7, 4:46 pm, Fattah <fattah.u...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You can write method to validate nested result attributes. > > Def valid_nested_results?(results_attributes) > > results_attributes.each do|res_attr| > return false if(res_attr[''attr1''].blank? and res_attr[''attr2''].blank? ....) > end > > return true > > end > > And use this method in reject_if instead of attributes[''results_attributes''].blank? > > Or you can also define the appropriate :reject_if for nested attribute definition for results in treatment model. > > Does it help? > > -----Original Message----- > From: Mlle <emsto...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Sent: Friday, 7 May 2010 7:35 PM > To: Ruby on Rails: Talk <rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > Subject: [Rails] Reject a nested object if all parameters of its own nested object are blank > > Hello, > > I have a model called Sample that has many Treatments. A Treatment > can have many Results. > > I want to reject the creation of a Treatment if all its fields are > blank and if all the fields of each of its nested Results are blank. > > I have this in the Sample model: > > accepts_nested_attributes_for :treatments, :allow_destroy => > true, :reject_if => proc { |attributes| > attributes[''treatmenttype''].blank? && attributes[''author''].blank? && > attributes[''results_attributes''].blank? } > > This part: > attributes[''results_attributes''].blank? > > only makes it reject the Treatment if there are no Results, but how > can I write the conditions so that it will reject the creation of a > Treatment if all the attirubtes of each Result are blank? Right now > it creates the Treatment if the Result exists, regardless of whether > all its fields are blank. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.