views:

216

answers:

4

Let's say I have four completely independent models (Movie, Book, Game, Album) that control the types of things I have in my media collection. With them I can CRUD and tag individual albums, movies etc.

But I need to keep track, and do some stuff that is common to, all of them. So I figured I need an Item model that would give me a table item like this:

| id | item_id | item_type | status | possession |
+----+---------+-----------+--------+------------+
| 01 |    01   |     1     |    3   |     2      |  

Where the status and possession bit would let me keep track of whether the item is new or used, with me or lent (to whom, in another table), etc, and the table itself would let me know how many items I have in total. All without touching the original four models and their objects, that I think should only have information about what they are, not what I can do to them. EDIT: Note that every time a movie or book is added, it also must update the items table with its related information.

I'm a newbie and I had some ideas on how to go about it, but none proved successful. I know it's a lot to ask but I would like to know, how can I accomplish this?

Any help will be appreciated, thanks.

+2  A: 

Inheritance in Rails (in my opinion) leave a lot to be desired, as it only allows single table inheritance.

What I would do, is relate each of your models (Movie, Book, etc) to an Item in a 1-to-1 relationship.

class Book < ActiveRecord::Base
  belongs_to :item
end

Edit: After taking a look at the polymorphic stuff (something I forgot that Rails had, to be honest), I think that might be more what you're looking for. My way would work, but I think the other way would work better.

Matt Grande
+4  A: 

The simplest way to do this is to use a polymorphic association.

Pesto
Thanks for the tip. I got the association working, but the problem is I can't get the items table to update automatically with the appropriate information whenever a movie, etc is added. I looked into callbacks and forms that submit to multiple models but didn't get far. What would you recommend?
Baby Diego
You want an observer: http://api.rubyonrails.org/classes/ActiveRecord/Observer.html
Sam C
+4  A: 

You are describing polymorphic associations in ActiveRecord. Check out these URLs:

gohanlon
Thanks for the tip. I got the association working, but the problem is I can't get the items table to update automatically with the appropriate information whenever a movie, etc is added. I looked into callbacks and forms that submit to multiple models but didn't get far. What would you recommend?
Baby Diego
+1  A: 

quick comments on attribute names ...

"Status" is IMHO horrible. Maybe "Acquisition type".

"Item type" is not too bad, but "Media type" might be more appropriate.

Here is a very comprehensive "state of the art" guide on entity naming conventions that would be worth skimming through at least. http://www.uscg.mil/directives/ci/5000-5999/CI_5230_42A.pdf

David Aldridge
Thanks for the resource, it will come in very handy. BTW, status was meant to refer to the condition of the item: new, used, old, etc. But maybe condition is a better name? (And I went with Item_type because I imagined that in associations like that the "foreign" fields must share the same "root".)
Baby Diego