This is a fairly typical polymorphic association. No need for the
''media_types'' table at all. Each media table has a link to one
photo,
or one video, or one song. Call it ''media_detail'' for lack of
a better
term.
class Media < ActiveRecord::Base
belongs_to :media_detail, :polymorphic => true
end
class Photo < ActiveRecord::Base # and same for Song and Video
has_one :media, :as => :media_detail
end
In your media table you have two columns: media_detail_type (string)
and media_detail_id (integer).
As for views, I would suggest a separate controller for each type, and
a shared partial view for the common fields.
It might be beneficial to use accepts_nested_attributes_for :media
inside each of Photo, Song, Video.
On Nov 5, 6:24 pm, danm <adwo...-qLMJZ3B/n2zQT0dZR+AlfA@public.gmane.org>
wrote:> Hi,
>
> I''m interested to know if anyone has any clever suggestions about
how
> to model the following scenario with ActiveRecord:
>
> - I''d like a collection of media, including photos, videos, and
songs,
> all of which have some common and unique properties
> - I start with a "media" table, that includes things like id,
creation
> date, name, description, and filename
> - the media table also has a media_type_id that maps to another table,
> media_types, that has IDs for each of the three types of media
> - then there are tables for each specific media type, so the photos
> table has photographer_name, subject_name, etc; the videos table has
> duration, format, etc; and the songs table has artist, duration, etc
> - each of the specific tables also has an ID column; a media_detail_id
> column in the media table maps to this ID
>
> My ultimate goal is for a URL like /media/index to display all the
> media names, and clicking on one gives the detail depending on what
> kind it is. Another goal is to be able to enter all the data for,
> say, a photo on one page -- both the attributes for the media table
> (name, description) and for the photos table (photographer name,
> subject name).
>
> I''ve been trying to work with Rails to do this simply, and
it''s clear
> I''m fighting against the grain of how it likes to do things. If
> anyone has any suggestions, they''d be much appreciated.
>
> Thanks,
> Dan