San Diago
San Diago

Reputation: 1050

Should I use simple or polymorphic associations in this case? [Rails]

Suppose I want to build something like tumblr, a kind of blog in which a post can be one of many kinds of content (text, quote, image, link, etc).

Is it ok for me to use simple associations, such as...

Class Blog < ActiveRecord::Base
  has_many :quotes
  has_many :images
  has_many :links
etc

Class Quote < ActiveRecord::Base
  belongs_to :blog
end

Class Image < ActiveRecord::Base
  belongs_to :blog
etc

Or do I have to use polymorphic associations? If so, what would it look like? If it's indifferent, what are the pros and cons of each approach?

Upvotes: 1

Views: 599

Answers (1)

sandrew
sandrew

Reputation: 3129

In your example I would recommend you to use Single Table Inheritance

Single table inheritance and where to use it in Rails

You have one entity with different types that have common behavior - it is exactly what STI was created for.

Upvotes: 2

Related Questions