Why should I use has_one association (and additional table, model), when I can embed associated object and use composed_of and/or serialize. In cases when I always join associated object and don’t need complex queries. For example:
Dog(color,weight) has_one :head Head(teeth_count,eye_color)
I can replace by Dog(color,weight,head_teeth_count,head_eye_color) and use “calculated attribute” or value objects like
composed_of :head, mapping: [%w(head_teeth_count teeth_count), %w(head_eye_color eye_color)]
Less tables, simpler, code clearer, less heavy models, no accidental extra queries, better performance.
Another example:
User(email) has_many :roles Role(name)
transforms into User(email,roles)
serialize :roles, Array
I missed something in these cases?
Semantics, which are actually a pretty important part of writing good software.
A book is not composed of an author. A book has an author.
I would also argue that you’re sorely mistaken about your example being simpler or having clearer code. The performance improvement is debatable, and likely negligible.
Your last case is particularly bad. What if you want to search for all users in a specific role? Instantiate all users, deserialize and check one at a time?