Rails database design with or without associations -
question database design rails.
i wanted know if i'm on right track or if i'm making things overtly complicated.
scenario: list of companies displays:
- company name
- bio
- website address
- phone number
- address
- social media links
i thought idea create 4 models backed database.
- companies
- numbers (a company can have multiple phone numbers (land, fax, pager)
- addresses (a company can have multiple locations)
- links (social media: facebook, google, linkedin
i have 2 questions.
- is okay have 1 table contains fields , keep on adding if required eg. new social media website. 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 - would breaking following make best practice, there performance benefits setting 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 # presume, create country , state model # , create has_many , belongs_to associations, # allow selections in dropdown box? end class socialmedialink < activerecord::base # table name = socialmedialinks name :string url: :string company_id :integer belongs_to :company end thanks reviewing question. also, if there rails open source project can suggest learn , @ how designed database / active record associations, please let me know.
i wanted know if i'm on right track
you are, it's valid question.
the biggest determinant of how you'd structure data have come scope of application.
if you're expecting company have multiple locations, social media profiles etc, yes, best splitting data. if expecting companies have singular data, you'd causing more problems worth.
extensibility
keep on adding if required
what's you're looking extensibility - ability add data system required.
if have company rigid data structure, you'd having single model/table. if wanted add fields (new social accounts etc), correct practice use multi-model approach. technically, there nothing wrong either.
--
the best thing recommend embed nested models 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 create dependent models if have need add records / data aforementioned models.
Comments
Post a Comment