Reputation: 1980
What I have right now is a Roleplay Model, and a Message Model.
I want them to share the same notification stream: They both should create a 'Notification' row which will then be displayed to the user. But I want the Notification also point to their parent. I thought about creating a parent_id and parent_class column so that I could get the parent object. But is there an already built in way of doing it?
I checked out Polymorphic Models, but I don't seem to really understand how to use it here.
Upvotes: 1
Views: 255
Reputation: 40333
The solution is exactly polymorphic models, if you add the parent_id and parent_class fields to the Notification model, you can easily create a polymorphic association at your notification model to implement it, here's how it would look:
class Notification < ActiveRecord::Base
belongs_to :parent, :polymorphic => true
end
Then to use it it's quite simple:
message = # assign the message here
notification = Notification.create(:parent => message)
Then you can just query the notifications and use it to be your source of all notifications.
Upvotes: 1