Raj
Raj

Reputation: 22926

Building a multi table relationship in Rails ActiveRecord

I have the following models in Rails:

Player
Match
Series

And a Post model:

Post

I want to link each post to one of the above model. Need to create a new table that will have

Object_type - player or match or series
Object_id - Id of above object
Post_id

Is there a built in way to create such relationship in rails? and easily access a player's post like player.posts? (that will filter for object_type = 'player' in this relationship table)

Upvotes: 0

Views: 91

Answers (2)

siddick
siddick

Reputation: 1478

Use Polymorphic Associations to manage relationships. Refer http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

Upvotes: 2

HarsHarI
HarsHarI

Reputation: 911

You can use STI (single table inheritance) for that for more detail read follow this link

http://www.therailworld.com/posts/18-Single-Table-Inheritance-with-Rails

Upvotes: 0

Related Questions