Currently I have a model that details a relationship between two people.
I.e.:
Relationship
t.references :person_one
t.references :person_two
When one of these people view the Relationship, they see the other person's name. Does it make more sense from a time-standpoint to store just the references? Or does it make more sense to do something like this:
Relationship
t.string :person_one
t.string :person_two
Each person is also able to view all the relationships which they're part of. I.e., every person has_many relationships.
It seems that t.references is just a way to abstract adding a person_id column, but it returns the desired Person object, rather than the id. I can definitely see the pros and cons of each. In the former, it seems like it may be easier (code-wise) to get all of a single person's relationships using person.relationships, but it also seems that a lot of behind-the-scenes querying seems to be happening under that implementation, and what I'm interested in is optimizing for time complexity/reducing the number of times I have to go through my SQL databases. But I may be misunderstanding the precise way in which Rails stores and then retrieves this data.
Thoughts?
If you build your application using best practices when it comes to normalisation of the database and appropriate use of eager loading, then the performance of the database will be of no concern to you.
Do not be misled into introducing redundant data, with person names being used in multiple tables to avoid using joins -- your data integrity problems and the efforts you will go through to (unsuccessfully) keep the data in synch will will be prohibitive, and wipe out any savings you might think you accrue through easier coding.