# Can you have a model with attributes of another model?

**URL:** <https://www.ruby-forum.com/t/can-you-have-a-model-with-attributes-of-another-model/191527>\
**Category:** Rails\
**Created:** [July 13, 2010, 11:25pm UTC](https://www.ruby-forum.com/t/can-you-have-a-model-with-attributes-of-another-model/191527 "2010-07-13T23:25:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakx12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jakx12](https://www.ruby-forum.com/u/jakx12)\
**Post date:** [July 13, 2010, 11:25pm UTC](https://www.ruby-forum.com/t/can-you-have-a-model-with-attributes-of-another-model/191527/1 "2010-07-13T23:25:03Z")

</div>

Ok so basically I have a problem. Im relatively new to rails and i seem  
to have come to a dead end. I wanted a model that could either be a URL,  
a video, a picture or a sound recording. Obviously this is impossible. I  
am gonna use attachment\_fu if that helps. So whats the most DRY way of  
doing this. Each media thing, whether it be a video, a picture, a URL or  
a sound file must be classified under the same thing as, lets say a  
media object. Consequently they share attributes, like an id, a upload  
date, a description etc. However they differ in their actual media  
format.

Does anyone know how I should make my models and controllers.  
Thanks!

---

<div class="post-metadata">

**Author:** ![jakx12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jakx12](https://www.ruby-forum.com/u/jakx12)\
**Post date:** [July 13, 2010, 11:43pm UTC](https://www.ruby-forum.com/t/can-you-have-a-model-with-attributes-of-another-model/191527/2 "2010-07-13T23:43:33Z")

</div>

On Tue, Jul 13, 2010 at 4:25 PM, Zack N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Does anyone know how I should make my models and controllers.  
> Thanks!

You probably want to use polymorphic associations.

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html#polymorphic-associations)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

–  
Greg D.  
[destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)

---

<div class="post-metadata">

**Author:** ![jakx12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jakx12](https://www.ruby-forum.com/u/jakx12)\
**Post date:** [July 13, 2010, 11:50pm UTC](https://www.ruby-forum.com/t/can-you-have-a-model-with-attributes-of-another-model/191527/3 "2010-07-13T23:50:24Z")

</div>

Greg D. wrote:

> On Tue, Jul 13, 2010 at 4:25 PM, Zack N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > Does anyone know how I should make my models and controllers.  
> > Thanks!
> 
> You probably want to use polymorphic associations.
> 
> [Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations)
> 
> –  
> Greg D.  
> [destiney.com](http://destiney.com) | [gregdonald.com](http://gregdonald.com)

hey thanks for the reply. Your right, I thought about it more and  
polymorphism is exactly what I need. However, I want a media object to  
be either a picture, a video, a sound file or a link. So that means  
links, pictures, videos and sound files have the attributes of a media  
object but are their own media. If that makes any sense?  
So for example id like to be able to do:  
MediaObject.find\_by\_video(example\_media\_object.content) or something  
like that. I dont really know how to explain this in rails, but I can do  
this easily in java haha.

Thanks.

---

<div class="post-metadata">

**Author:** ![jakx12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jakx12](https://www.ruby-forum.com/u/jakx12)\
**Post date:** [July 14, 2010, 10:15am UTC](https://www.ruby-forum.com/t/can-you-have-a-model-with-attributes-of-another-model/191527/4 "2010-07-14T10:15:05Z")

</div>

Zack,

Tuesday, July 13, 2010, 3:25:03 PM, you wrote:

ZN\> Ok so basically I have a problem. Im relatively new to rails and i  
seem  
ZN\> to have come to a dead end. I wanted a model that could either be a  
URL,  
ZN\> a video, a picture or a sound recording. Obviously this is  
impossible. I  
ZN\> am gonna use attachment\_fu if that helps. So whats the most DRY way  
of  
ZN\> doing this. Each media thing, whether it be a video, a picture, a  
URL or  
ZN\> a sound file must be classified under the same thing as, lets say a  
ZN\> media object. Consequently they share attributes, like an id, a  
upload  
ZN\> date, a description etc. However they differ in their actual media  
ZN\> format.

ZN\> Does anyone know how I should make my models and controllers.  
ZN\> Thanks!  
ZN\> –  
ZN\> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Two possibilities come to mind: Subclassing and Mixins.

You can create

* * *

class MediabBase \< ActiveRecord::Base  
#attributes  
end

class VideoMedia \< MediaBase  
#more attributes  
end

class JpegMedia \< MediaBase  
#other attributes  
end

* * *

Ill let someone else explain how to do Mixins.

Caution: I’ve been doing Rails for about 6 months but still consider  
myself a novice. My answer may be grossly wrong.

---

<div class="post-metadata">

**Author:** ![jakx12](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jakx12](https://www.ruby-forum.com/u/jakx12)\
**Post date:** [July 14, 2010, 12:53pm UTC](https://www.ruby-forum.com/t/can-you-have-a-model-with-attributes-of-another-model/191527/5 "2010-07-14T12:53:03Z")

</div>

Ok thanks!
