Forum: Ruby on Rails Pattern for a unified search of multiple similar models

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 kl (Guest)
on 2007-03-29 14:11
I'm trying to come up with a way of providing some kind of way in which
I can do a find over a number of different models which have common

For example say I have two models Dog and Cat, they both have some
fields the same (eg. name, age, weight) but also additional differing
fields. Maybe Cat and Dog models are independant or maybe they are
inhertied from the same base model, Animal.

If I wanted to do a find on name, but wanted to do so for Cats and Dogs,
and provide the results to the view as a single aggregated array of type
Animal how would I do this?
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 kl (Guest)
on 2007-03-29 14:20
Okay two things I have looked at already:

STI - problem is that in the case of Cat and Dog this might work but
there possibility might be Horse, Cow, Chicken, all with loads of
non-common fields. Which would get pretty messy.

Polymorphic Assosiations - Would this work if I had an Animal model
which held the common fields and with animal_id and animal_type fields.
The logic would mean when creating a new Dog, I would also need to
always create a matching new Animal?
This topic is locked and can not be replied to.