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? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 19, 9:59 pm, Solidify <max.holtm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> 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?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Why don''t you just make both fields plain text attributes of the author object? On 7/19/10 5:53 PM, Frederick Cheung wrote:> > On Jul 19, 9:59 pm, Solidify<max.holtm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 >> 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?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Jul 19, 9:59�pm, Solidify <max.holtm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > FredI see you read my mind while I was writing my reply. :) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 July 2010 10:30, Sampath Munasinghe <sampathnisha-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 19, 11:59 pm, Solidify <max.holtm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > 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?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 20, 11:00 am, Eduard Martini <eduard.mart...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 >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 Jones -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.