rails models for address books (company, person, address)

旧城冷巷雨未停 提交于 2020-01-17 01:42:29

问题


I want to create address-book-like set of models to represent company, person, location, etc. This looks like very typical address book, I wonder if somebody did it already with ruby on rails 3. The question appeared not (only) because of my laziness, but also because "best practice" approach is usually well-developed, have fewer pitfalls, etc. Currently I think about following models/fields:

Company:

  • name
  • has_many :persons
  • has_many :locations
  • has_many :urls, :through => :urlcatalog

    the reason to have URL catalog is a possibility to assign notes to URL

Person:

  • name maybe split to first-middle-last
  • phone
  • has_one :location
  • has_many :emails :through => :emailcatalog

    the reason to have email catalog is the same as above: one can assign "private", "office" labels to it. many phones can be organized this way as well.

Location

  • address optional
  • has_one :city
  • has_one :country

    city or country should be present

City

  • name
  • has_one :country

Country

  • name

any comments on this concept, thoughts, working examples, etc are welcome!


回答1:


You should look up polymorphic associations. This means your location will be better organized and it will be easier to get parent etc...

Good example over at RailsWiki



来源:https://stackoverflow.com/questions/6140215/rails-models-for-address-books-company-person-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!