hello friends, I need to modelate a genealogical tree with: animal,Dam(Mother) and Sire(Father) the Dam and Sire are Animals then i modelate this: class Animal < ActiveRecord::Base belongs_to :sire belongs_to :dam validates_associated :sire, :dam end class Sire < Animal has_many :animals end class Dam < Animal has_many :animals end then when i save a new animal the validate doesnt work like this: i have the table Animals ID, Name Father MOther 1 A - - 2 B - - when I insert a new record like this insert(3,C,1,2) its ok! insert(3,C,5,7) its ok too, (the validates is doesnt work) how can i solve this?? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alfredo Gama wrote:> hello friends, > > I need to modelate a genealogical tree with: > animal,Dam(Mother) and Sire(Father) > > the Dam and Sire are Animals then i modelate this: > > class Animal < ActiveRecord::Base > belongs_to :sire > belongs_to :dam > validates_associated :sire, :dam > end > > class Sire < Animal > has_many :animals > end > > class Dam < Animal > has_many :animals > end > > then when i save a new animal the validate doesnt work like this: > > i have the table Animals > > ID, Name Father MOther > 1 A - - > 2 B - - > > when I insert a new record like this > > insert(3,C,1,2) its ok! > insert(3,C,5,7) its ok too, (the validates is doesnt work) > > how can i solve this??Use validates_presence_of instead of validates_associated. Or use in addition to validates_associated if you want to check some attributes of the associates. -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James wrote:> Alfredo Gama wrote: >> validates_associated :sire, :dam >> then when i save a new animal the validate doesnt work like this: >> insert(3,C,5,7) its ok too, (the validates is doesnt work) >> >> how can i solve this?? > > Use validates_presence_of instead of validates_associated. > > Or use in addition to validates_associated if you want to > check some attributes of the associates. > > > -- > We develop, watch us RoR, in numbers too big to ignore.Thanks friend. the problem is that in some cases an Animals has not father or mother, then how can i do it?? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alfredo Gama wrote:>> Use validates_presence_of instead of validates_associated. > > the problem is that in some cases an Animals has not father or mother, > then how can i do it??This should allow clones if your foreign_keys are default NULL: validates_presence_of :sire, :if => Proc { |animal| animal.sire_id } validates_presence_of :dam, :if => Proc { |animal| animal.dam_id } If instead they''re default 0, the condition becomes animal.sire_id > 0 etc. -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James wrote:> Alfredo Gama wrote: > >>> Use validates_presence_of instead of validates_associated. >> >> the problem is that in some cases an Animals has not father or mother, >> then how can i do it?? > > This should allow clones if your foreign_keys are default NULL: > > validates_presence_of :sire, :if => Proc { |animal| animal.sire_id } > validates_presence_of :dam, :if => Proc { |animal| animal.dam_id } > > If instead they''re default 0, the condition becomes > > animal.sire_id > 0 > > etc. > > -- > We develop, watch us RoR, in numbers too big to ignore.THanks a lot for your help, what is Proc?? i have to write the Proc method?? i newbie in RoR sorry -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alfredo Gama wrote:>> validates_presence_of :sire, :if => Proc { |animal| animal.sire_id } > > what is Proc?? i have to write the Proc method??A Proc is a mini function that doesn''t have a name. You can pass them around as parameters to methods. See: http://www.ruby-doc.org/core/classes/Proc.html Note that I incorrectly capitalized "proc". You instead have to write either proc { |animal| animal.sire_id } or Proc.new { |animal| animal.sire_id } -- 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 -~----------~----~----~----~------~----~------~--~---