Hello, maybe you can help me. I am looking for a reasonable way to model the following concept. Assuming, we have 2 entities: 1. User (table users) 2. Town (table towns) A user can, for example, add a town to the database, so we have 1:n relationship (one user can add several towns) and the corresponding foreign key in the town database, that''s allright. But now, let''s suppose, that a user can optional be a kind of contact person for one or more towns. What''s the right way to realize this? I thought about an attributed join table between users and towns. Since habtm doen not support attributes to be changed (as far as i know), we could realize it through has_many :through... But now, I ask myself if that''s the best way to model the relationship between users and towns and I really asked myself, how to name this join table. Normally, it''s common practice to name tables after nouns, in this case maybe contact_persons. But I could it also name something like "acts_as_contact_person_for" to get a bit more straight forward. I really would like to have a good solution for this, because, I''ll have a lot of such relations. In conclusion: Do I generally have the right idea about that? How would you model this? And what''s about the naming in this case? Is there a kind of pattern for this problem? I really thank you a lot! :) Best wishes, ms --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
User can optional be a kind of contact person for one or more towns Please explain this more detail. On 29 окт, 21:48, ms <m...-cGBD8117FJM@public.gmane.org> wrote:> Hello, > > maybe you can help me. I am looking for a reasonable way to model the > following concept. > > Assuming, we have 2 entities: > 1. User (table users) > 2. Town (table towns) > > A user can, for example, add a town to the database, so we have 1:n > relationship (one user can add several towns) and the corresponding > foreign key in the town database, that''s allright. But now, let''s > suppose, that a user can optional be a kind of contact person for one > or more towns. What''s the right way to realize this? > > I thought about an attributed join table between users and towns. > Since habtm doen not support attributes to be changed (as far as i > know), we could realize it through has_many :through... > > But now, I ask myself if that''s the best way to model the relationship > between users and towns and I really asked myself, how to name this > join table. Normally, it''s common practice to name tables after nouns, > in this case maybe contact_persons. But I could it also name something > like "acts_as_contact_person_for" to get a bit more straight forward. > I really would like to have a good solution for this, because, I''ll > have a lot of such relations. > > In conclusion: Do I generally have the right idea about that? How > would you model this? And what''s about the naming in this case? Is > there a kind of pattern for this problem? > > I really thank you a lot! :) > > Best wishes, > ms--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Thorsten Müller
2008-Oct-30 10:29 UTC
Re: How would you model this? Thanks for any suggestions!
Sounds good to me. Would do it the same way. The intermediate table name is always a nasty decision to make. I wouldn''t use the acts_as variant, since that''s used by plugins and could be confusing. I would go for either "users_towns" or "user_town_contacts" Most likely for the user_town_contacts variant, if there is any chance, that users and towns will in future get more kinds of relations. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I understand! Looking this: User has many contacts Contacts has field town Then to find user contacts in town write User.contacts.find(:town => ''Yellow'')'' Why not? On 30 окт, 06:29, Thorsten Müller <thors...-1oxKqHKwyltBDgjK7y7TUQ@public.gmane.org> wrote:> Sounds good to me. Would do it the same way. > The intermediate table name is always a nasty decision to make. > I wouldn''t use the acts_as variant, since that''s used by plugins and > could be confusing. > I would go for either "users_towns" or "user_town_contacts" > Most likely for the user_town_contacts variant, if there is any > chance, that users and towns will in future get more kinds of > relations.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
you could model users polymorphic, such that users are either operators or contacts. then you would be able to say that a town belongs to a operator and has many contacts. http://cheat.errtheblog.com/s/belongs_to/ http://cheat.errtheblog.com/s/has_many/ hope this helps --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---