Re: has_many

Alex Young
2005-09-05T15:42:56Z

Bogdan Ionescu wrote:
> I have the Human class.
> In the humans table i have the fields mother_id and father_id
> I would like to define in human.rb that:
>
> has_many :children, :class_name=>''Human''
>
> but I can''t figure out how to specify the foreign_keys. Initially
I was
> using f_children and m_children, and added them to obtain the children, but
> I wonder if it''s a way to get the children directly.

Surely you want:

class  Human < ActiveRecord::Base
end

class Mother < Human
	has_many :children, :class_name => ''Human'', :foreign_key
=> ''mother_id''
end
class Father < Human
	has_many :children, :class_name => ''Human'', :foreign_key
=> ''father_id''
end

and have a ''type'' field in the Human table?  Except under
rather unusual
circumstances, a Human shouldn''t be capable of being both a Father and
a
Mother, after all...

By the way, does anyone else have any smart ideas as to how to achieve
this with acts_as_tree?

--
Alex