I have: Kids: mother_id father_id Parent: gender has_one :kid, :foreign_key=>mother_id or father_id based on parent.sex So a Parent has only one Kid, but I want it initialized based on the gender of the parent. Can this be done with has_one? Thanks _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 8.10.2005, at 21.33, Bogdan Ionescu wrote:> I have: > > Kids: > mother_id > father_id > > Parent: > gender > has_one :kid, :foreign_key=>mother_id or father_id based on > parent.sex > > So a Parent has only one Kid, but I want it initialized based on > the gender of the parent. Can this be done with has_one?Do you really, really want to restrict a person to only be able to have one child? I didn''t think so :-) So has_one is not the way to do this because it''s meant for one-to- one relationships. What you probably want is something like this: Person < AR::Base belongs_to :mother, :class_name => "Person", :foreign_key => "mother_id" belongs_to :father, :class_name => "Person", :foreign_key => "father_id" has_many :m_children, :class_name => "Person", :foreign_key => "mother_id" has_many :f_children, :class_name => "Person", :foreign_key => "father_id" def children m_children + f_children # one of these is always empty so the result # will depend on whether this person is a mother # or a father end end //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Oct 9, 2005, at 9:29 AM, Jarkko Laine wrote:> On 8.10.2005, at 21.33, Bogdan Ionescu wrote: > >> I have: >> >> Kids: >> mother_id >> father_idWouldn''t it make more sense to eliminate the separation of mother and father in the DB, and determine motherliness or fatherliness via a sex field? The way it stands, I think you''re going to need to code everything twice that deals with parents. -- -- Tom Mornini
On 10/9/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > Do you really, really want to restrict a person to only be able to > have one child? I didn''t think so :-)Actually that is exactly what I want. Person/Kid was just an example. So the restriction is in place.> > def children > m_children + f_children # one of these is always empty so > the result > # will depend on > whether this person is a mother > # or a father > endWell, I have: class Horse < ActiveRecord::Base has_one :unborn_mhorse,:foreign_key=>"mother_id",:class_name=>"UnbornHorse" has_one :unborn_fhorse,:foreign_key=>"father_id",:class_name=>"UnbornHorse" def unborn_horse return self.unborn_fhorse if self.gender== $HORSE_GENDERS[0] return self.unborn_mhorse if self.gender== $HORSE_GENDERS[1] end but, I was hoping that this could be done in a more elegant way, without using the extra method. Bogdan _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Basically I have a ''Human'' table that holds a number of ''humans'' (males/females) The ''kids'' table is different, it holds some temporary data, referencing 2 ''humans''. a mother and a father So what I need is to find if a human(male or female) has a kid. On 10/9/05, Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> wrote:> > On Oct 9, 2005, at 9:29 AM, Jarkko Laine wrote: > > > On 8.10.2005, at 21.33, Bogdan Ionescu wrote: > > > >> I have: > >> > >> Kids: > >> mother_id > >> father_id > > Wouldn''t it make more sense to eliminate the separation of mother and > father in the DB, and determine motherliness or fatherliness > via a sex field? > > The way it stands, I think you''re going to need to code everything > twice that deals with parents. > > -- > -- Tom Mornini > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 10/9/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> > > has_many :m_children, :class_name => "Person", :foreign_key => > "mother_id" > has_many :f_children, :class_name => "Person", :foreign_key => > "father_id" > > def children > m_children + f_children # one of these is always empty so > the result > # will depend onFor children I use: has_many :children,:class_name=>"Horse",:finder_sql=>''select * from horses where mother_id=#{id} OR father_id=#{id}'' I think its cleaner than your method (which I was initially using too) bogdan _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails