RnR: Database normalization, rails models and associations


Ok, so I have three different objects: a person, place, and equipment. Each can have an address, in most cases multiple address and multiple phone numbers. So my thought is create the specific object tables, the have an address and a phone table. Here my question:

So typically, in my SQL world, I would just have an object_id column in the address and phone table and put a id of the object in the object id column, and then select all the address or phone records that match.

I could do this, do find_by_sql and get the records but I'd rather stay within the ActiveRecord paradigm. So does that mean I should have an id column in the table for each object, so a person_id, place_id, etc.

What's the "right" way to model this?


You can create polymorphic associations with activerecord, so you can add object_id and object_type to the addresses and phone_numbers tables, and specifying the associations like this:

class Address belongs_to :object, :polymorphic => true end class PhoneNumber belongs_to :object, :polymorphic => true end class Person has_many :addresses, :as => :object has_many :phone_numbers, :as => :object end class Place has_many :addresses, :as => :object has_many :phone_numbers, :as => :object end class Equipment has_many :addresses, :as => :object has_many :phone_numbers, :as => :object end

This way, Person.first.addresses should run a query like Addresses.where(:object_type => "Person", :object_id => Person.first.id), and vice versa for the rest of the models and associations.


  • Getting an error when using COLLATE in a Mysql query [closed]
  • Rails 3 ActiveRecord: UNION
  • mysqli_query(): Couldn't fetch mysqli while testing a PHP function in PHPUnit
  • Limits for executing stored procedure with parameter through Excel ADO connection
  • SQL Server 2008 - Concatenate String
  • Ruby on Rails Tutorial - Michael Hartl - Test “User when email address is already taken”
  • Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded
  • Find record, that has ALL associated records
  • Rails 4 order by virtual attribute
  • Wordpress - Delete all tags by Post ID
  • Rails CarrierWave versions are not created for some reason
  • ruby/rails: extending or including other modules
  • iOS - Is this a task for enums?
  • DataGridView: Pass by Value or Reference?
  • Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • Action Pack components in Rails
  • pickle.PicklingError: args[0] from __newobj__ args has the wrong class with hadoop python
  • Using $compile in a directive triggers AngularJS infinite digest error
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • pyodbc doesn't report sql server error
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • Android full screen on only one activity?
  • Java: can you cast Class into a specific interface?
  • Ajax Loaded meta Tags
  • How to access EntityManager inside Entity class in EJB3
  • Control modification in presentation layer
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Xamarin Forms - UWP Fonts
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • AES padding and writing the ciphertext to a disk file
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Where to put my custom functions in Wordpress?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Arrays break string types in Julia