0

I am needing to create a gallery containing entries from two different models/tables, let's say "video" and "image". What is the best way to handle this? I would like to keep them in different tables, but retrieve them together (the most recent 50 images and videos, for example). Single Table Inheritance doesn't seem to fit. Any ideas?

2 Answers 2

1

Polymorphism is your friend.

class Video
  belongs_to :viewable, :polymorphic => true
end

class Image
  belongs_to :viewable, :polymorphic => true
end

class Gallery
  has_many :media, :as => :viewable
end
Sign up to request clarification or add additional context in comments.

Comments

0

The best way I have been able to figure is to have a table of all the displays with polymorphic references to the other tables.

class Gallery
  has_many :displays
end

class Display
  belongs_to :gallery
  belongs_to :derived_display, :polymorphic => true
end

class Video
  has_one :display, :as => :derived_display
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.