Forum: Ruby on Rails creating a model that has other models as elements

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
martin (Guest)
on 2006-02-19 20:39
(Received via mailing list)
Say I have the following three tables:

dogs
-------
id (PK)
name

cats
------
id (PK)
name

recipies
-----------
id (PK)
recipie_text

I've made a model for each of them: Dog, Cat and Recipie.

Now I want to create a model, Cookoff, that will contain arrays of
Dog, Cat and Recipie objects. There is no associated database table
for Cookoff because it's merely a container for a specific request,
not an actual data item.

My users will submit a form with the following data:

dog_id: 10
cat_id: 12

A Cookoff instance will be created, with the following parameters:

- a Dog model object with data from the dogs table where id=dog_id
- a Cat model object with data from the cats table where id=cat_id
- a Recipe model object with data from the recipies table where the
recipie_text contains Dog.name or Cat.name

The Cookoff instance would then be available for such things as:

Cookoff.Dog.name
Cookoff.Recipie[0].recipie_text
etc.

What would model/cookoff.rb look like?

I'm still getting started in both Ruby and Rails, so I hope I have
been clear in my question.

Thank you again for the help!
This topic is locked and can not be replied to.