Many to many relations

Hi everyone, I have a simple question:

an author has a place of birth and a place of death. Both places
should come from the same table (cities). This should be a
many_to_many relationship, because one author may be born in the same
city where the other author dies.

Now in the authors-table, one city_id would not be enough, it has to
be two fields, one for birth, one for death. but then, the
@author.city would not work anymore. Could anyone give me a hint how
to solve this?

On Jul 19, 9:59 pm, Solidify [email protected] wrote:

Hi everyone, I have a simple question:

an author has a place of birth and a place of death. Both places
should come from the same table (cities). This should be a
many_to_many relationship, because one author may be born in the same
city where the other author dies.

If I were you I’d have two belongs_to associations, one for the
birthplace and one for the death location. I don’t think many-to-many
is appropriate, unless multiple locations can be associated with a
person’s birth/death

Fred

Why don’t you just make both fields plain text attributes of the
author object?

Frederick C. wrote:

On Jul 19, 9:59�pm, Solidify [email protected] wrote:

Hi everyone, I have a simple question:

an author has a place of birth and a place of death. Both places
should come from the same table (cities). This should be a
many_to_many relationship, because one author may be born in the same
city where the other author dies.

If I were you I’d have two belongs_to associations, one for the
birthplace and one for the death location. I don’t think many-to-many
is appropriate, unless multiple locations can be associated with a
person’s birth/death

Fred

I see you read my mind while I was writing my reply. :slight_smile:

Solidify wrote:

Hi everyone, I have a simple question:

an author has a place of birth and a place of death. Both places
should come from the same table (cities). This should be a
many_to_many relationship, because one author may be born in the same
city where the other author dies.

That’s not a good spot for a many-to-many relationship. Rather, it’s
better modeled as two one-to-many relationships, where an author belongs
to one city as birthplace and one city as “deathplace”. Since the place
of birth and the place of death are semantically different, they should
not be shoved into the same association.

A better candidate for a many-to-many relationship would be a list of
places where an author resided throughout his life. Since they’re
semantically the same, it makes sense to do many-to-many here.

Now in the authors-table, one city_id would not be enough, it has to
be two fields, one for birth, one for death. but then, the
@author.city would not work anymore. Could anyone give me a hint how
to solve this?

@author.city is semantically meaningless. It shouldn’t be defined
from what you’ve said. You probably want something like (untested):

class Author < AR::B
belongs_to :birth_city, :class_name => ‘City’
belongs_to :death_city, :class_name => ‘City’
end

class City < AR::B
has_many :authors_born, :class_name => ‘Author’, :foreign_key =>
:birth_city_id
has_many :authors_died, :class_name => ‘Author’, :foreign_key =>
:death_city_id
end

Then you can use @author.birth_city and @author.death_city.

Does that help?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Try
class author
has_one birth :through city
has_one death :through city

class city
belongs_to death
belongs_to birth

class birth
has_one city

class death
has_one city

Anybody having good tutorial for many to many relationships? I need to
do
following.

categories can have many items
item can belongs many categories

i need to save all the categories and items from single form.

please help me.

On 20 July 2010 10:30, Sampath M. [email protected]
wrote:

Anybody having good tutorial for many to many relationships? I need to do
following.
categories can have many items
item can belongs many categories
i need to save all the categories and items from single form.
please help me.

You do not say what level your are at. Start with the ActiveRecord
Relatinships guide at http://guides.rubyonrails.org/

Colin

in authors model:

has_one: city_of_birth, :table => cities
has_one :city_of_death, :table => cities

in authors table:
city_of_birth_id, city_of_death_id

author instance:

@author.city_of_birth
@author.city_of_death

On Jul 20, 11:00 am, Eduard M. [email protected] wrote:

@author.city_of_birth
@author.city_of_death

You’ve got those backwards - they should both be belongs_to, as the
foreign key lives in the authors table not the cities table.

I’d second the question about why these can’t just be plain text
fields; there are a huge number of cases where a person could live in
exactly the same house their entire life and still manage to be born
in one city but die in another. For that matter, be born in one
country and die in another without leaving the house…

Your cities table is likely to be full of semi-duplicate entries that
vary with time - as a concrete example: I live in Columbus, Ohio.
Before 1812, there was no such city. Well after the city’s founding,
additional areas (Franklinton, for instance) that used to be
independent cities were annexed into Columbus.

Bonus points if you can figure out how to represent authors whose
birthplace is not a matter of settled historical fact (primarily an
issue with some pre-20th century authors)…

–Matt J.

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