# ActiveRecord model help

**URL:** <https://www.ruby-forum.com/t/activerecord-model-help/131084>\
**Category:** Rails\
**Created:** [March 5, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/activerecord-model-help/131084 "2008-03-05T22:25:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@Jonathon](https://www.ruby-forum.com/u/Jonathon)\
**Post date:** [March 5, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/activerecord-model-help/131084/1 "2008-03-05T22:25:34Z")

</div>

Hey guys,

I’m not that great with Rails yet, but I’m trying to get there.

I’m developing a site where users will be able to upload pictures and  
videos (eventually). I want to set the pictures and the videos up as  
separate models, or at least so they will act like it. Comments, tags,  
and ratings will go onto the pictures and videos. I’ve decided the  
easiest way to do it, while keeping everything nice and DRY, is to  
create a “content\_item” model, through which the pictures and videos  
will act.

I’m trying to find a way to basically do the following:  
Picture.find(1) will go find the item with id = 1 in the content\_items  
model. If the content\_type at id 1 isn’t a picture, it will return an  
error.  
Video.find\_by\_username(“Foobar”) would return all videos for the user  
Foobar by using content\_type “Video”

I don’t think polymorphic associations applies to this, since the  
content doesn’t has\_one video, the content IS the video, if that makes  
any sense.

As of right now, I have a blank model. I know the answer is something  
simple, but I just can’t seem to find it.

Thanks,

Jonathon

---

<div class="post-metadata">

**Author:** ![Jonathon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@Jonathon](https://www.ruby-forum.com/u/Jonathon)\
**Post date:** [March 5, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/activerecord-model-help/131084/2 "2008-03-05T22:46:16Z")

</div>

What about single table inheritance?

You can add two more models. One for Video and one for Picture and  
have them inherit from ContentItem.

I have a User model in my app and two other models which use the same  
table: Employee and Customer. I created two new files in the models  
folder: customer.rb and employee.rb.

Here is what employee.rb looks like:

class Employee \< User

# model specific code here

end

you can do Employee.find(1) and it will return an Employee object from  
the users table.

---

<div class="post-metadata">

**Author:** ![Jonathon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@Jonathon](https://www.ruby-forum.com/u/Jonathon)\
**Post date:** [March 5, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/activerecord-model-help/131084/3 "2008-03-05T22:49:49Z")

</div>

just wanted to add when you do something like Video.find(1) and item 1  
is a picture it will raise ActiveRecord::RecordNotFound

you can just run some code in your controller to handle errors from  
there.
