Apologies if this question is a bit daft, but are there any negative repercussions if an app has a model or models with numerous associations? For a complex app requiring a User model, for example (eg. a social networking site), it’s plausible that the model could have 15+ associations (has_many :posts, has_many :messages, has_many :photos, has_many :friends and so on). If one model is heavily associated to others, does this have any negative effect on the performance of the app? And if so, what is the best way to go about minimising problems?
Apologies if this question is a bit daft, but are there any negative repercussions
Share
An association between Models in Rails is really just manipulating id values behind the scenes. These are pretty high-performance operations, especially if you’ve taken the time to set up foreign-key relationships if your DB supports it. ActiveRecord also doesn’t load anything unless you ask for it, so normally they would be translated as a JOIN for the SQL server only if you’re acting on those relationships.
As the posts, messages, photos, or friends in your example are created, ActiveRecord will set the
user_idcolumn for you automatically. That’s all that is involved in ahas_manyassociation.Premature optimization is the root of all evil. Don’t worry about it until you’ve established a bottleneck with profiling.