rails models for address books (company, person, a

2019-09-02 09:10发布

问题:

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