0

In terms of performance, is it more efficient to use one large table as opposed to several smaller tables?

@animals = Animal.where(user_id: 1)

OR

@animals = Cat.where(user_id: 1) + Dog.where(user_id: 1)

Thank you very much.

colincr
  • 550
  • 1
  • 7
  • 25
Mr McDonald
  • 453
  • 7
  • 16

1 Answers1

2

In this particular case you are questioning about (which can be applied to similar data modeling), one table would be better. The way I'd design the model Animal is for it to have an attribute species for example.

Getting all dogs and cats would simply be:

Animal.where("species = 'dog' OR species = 'cat'")

Or you can use the Rails 5 or method like so

Animal.where(species: 'dog').or(Animal.where(species: 'cat'))

Take a look at Active Record query interface

Cyzanfar
  • 6,997
  • 9
  • 43
  • 81