New to rails, not sure how to model this

Well, as in the title, I’m pretty new to rails, though I’ve been messing
around with it on and off for a few months. nothing big, just some test
projects with everything pretty simple.

Well, right now I’m trying to put something together and I’m having
trouble conceptualizing just how to model things. I’m putting together
what basically amounts to a family tree - I have people, and I have
relationships between people (ie, a father may have a daughter or two
and a wife). I’ve tried person has_many :relationships and relationship
belongs_to :person, but that seemed to just confuse me and my app.
Likewise, I had trouble putting together a coherent db schema, as a
relationships table would need two different person_id’s (continuing the
example, one for the father, and one for his daughter). With this app, I
would need to be able to, when pulling a page up about a certain person,
have links with names to those people that the person has relationships
with.

Any suggestions? I’m new to rails, so any advice is welcome.

Check out acts_as_tree. It may be able to help you b/c it enables you
to model hierarchies.

In person.rb you would have:

class Person < ActiveRecord::Base
acts_as_tree :order=>“age” <-- or however you want to sort it
end

Make sure that in your schema you have a field called parent_id.
Somethin like:
create table people (
id int not null auto_increment,
parent_id int ,
name varchar(200) not null,
created_on datetime not null,
updated_on datetime not null,
constraint fk_person_parent foreign key (parent_id) references
people(id),
primary key (id)
);

When searching you can use an idiom I don’t quite remember to pull up
all the children as well.

That’s perfect according to the description I gave, thanks a lot! Looked
up the proper stuff for calling up the children from the page, and it
works just as advertised.
(found info on it here:
http://api.rubyonrails.org/classes/ActiveRecord/Acts/Tree/ClassMethods.html)

However, if I were to expand this to a Father + Mother both being able
to branch down to the same children (as families tend to work ;), how
would I do this? Is there something similar to acts_as_tree that allows
for multiple parents?

Thanks again :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs