Wasabi Developer
Wasabi Developer

Reputation: 3553

Rails database design with or without associations

Question about database design with Rails.

I wanted to know if I'm on the right track or if I'm making things overtly complicated.

Scenario: A list of companies that displays:

I thought it would a good idea to create 4 models backed by a database.

I have two questions.

  1. Is it okay to simply have 1 table that contains all the fields and keep on adding to it if required eg. new social media website. Is this bad practise?

class Company < ActiveRecord::Base
  # Table name = Companies
  # name     :string
  # bio      :text
  # url      :string
  # email    :string
  # phone    :string
  # Location information
  # address1 :string
  # address2 :string
  # city     :string
  # state    :string
  # postcode :string
  # country  :string
  # Social media links
  # facebook :string
  # google   :string
  # linkedin :string
end

  1. Would breaking it up like the following make for best practice, is there any performance benefits setting it up this would with foreign keys / has_many, belongs_to associations?

class Company < ActiveRecord::Base
 # Table name = Companies
 # name     :string
 # bio      :text
 # url      :string
 # email    :string
 # phone    :string

  has_many :locations
  has_many :social_media_links
end

class Number < ActiveRecord::Base
  # Table name = Numbers
  number      :string
  company_id  :integer

  belongs_to :company
end

class Location < ActiveRecord::Base
  # Table name = Locations
  # address1     :string
  # address2     :string
  # city         :string
  # state        :string
  # postcode     :string
  # country      :string
  # company_id   :integer

  belongs_to :company

  # I presume, you could also create a Country and State model
  # and create a has_many and belongs_to associations, 
  # to allow for selections like in a dropdown box?
end

class SocialMediaLink < ActiveRecord::Base
  # Table name = SocialMediaLinks
  name        :string
  url:        :string
  company_id  :integer

  belongs_to :company
end

Thanks for reviewing this question. Also, if there are Rails open source project that you can suggest to learn and look at how they designed their database / active record associations, please let me know.

Upvotes: 0

Views: 136

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

I wanted to know if I'm on the right track

You are, it's a valid question.

The biggest determinant of how you'd structure the data would have to come from the scope of the application.

If you're expecting a Company to have multiple locations, social media profiles etc, then yes, you would be best splitting the data. If you were only expecting companies to have singular data, then you'd just be causing yourself more problems than its worth.


Extensibility

keep on adding to it if required

What's you're looking for is extensibility - the ability to add data to your system as required.

If you have a company with rigid data structure, you'd do well having a single model/table. If you wanted to add extra fields (new social accounts etc), the correct practice is to use the multi-model approach. Technically, there is nothing wrong with either.

--

The best thing I would recommend would be to embed nested models into modules:

#app/models/company.rb
class Company < ActiveRecord::Base
  has_many :locations, class_name: "Company::Location", dependent: :destroy
end

#app/models/company/location.rb
class Company::Location < ActiveRecord::Base
  belongs_to :company
end

You should only create the dependent models if you have the need to add extra records / data for the aforementioned models.

Upvotes: 1

Related Questions