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! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ralph Shnelvar
2010-Jul-13  21:35 UTC
Re: Can you have a model with attributes of another model?
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/. 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Greg Donald
2010-Jul-13  21:41 UTC
Re: Can you have a model with attributes of another model?
On Tue, Jul 13, 2010 at 4:25 PM, Zack Nathan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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!You probably want to use polymorphic associations. http://guides.rubyonrails.org/association_basics.html#polymorphic-associations -- Greg Donald destiney.com | gregdonald.com -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Zack Nathan
2010-Jul-13  21:50 UTC
Re: Can you have a model with attributes of another model?
Greg Donald wrote:> On Tue, Jul 13, 2010 at 4:25 PM, Zack Nathan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >> Does anyone know how I should make my models and controllers. >> Thanks! > > You probably want to use polymorphic associations. > > http://guides.rubyonrails.org/association_basics.html#polymorphic-associations > > > -- > Greg Donald > destiney.com | gregdonald.comhey 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Zack Nathan
2010-Jul-14  10:53 UTC
Re: Can you have a model with attributes of another model?
Ok thanks! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.