Reputation: 4566
I want to make a simple user notification in rails with just text. I want there to be multiple notifications obviously so my question is do I have to create a whole new migration and create an association with my User model or is there some way to just extend a "notifications" text attribute in my User table? I guess you can just insert line breaks in between notifications but that's not what I'm looking for as that sounds like it would get too messy. Thanks!
Upvotes: 0
Views: 476
Reputation: 5110
If you just want to store notifications only you can tell Rails to treat it as an Array (notifications' column type should be text
):
class User < ActiveRecord::Base
serialize :notifications, Array
end
If you want to store any additional notification details (like author or timestamps or whatever) you should probably go for a separate model.
Rails docs: Active Record, search for "Saving arrays, hashes, and other non-mappable objects in text columns".
Upvotes: 2
Reputation: 2987
If you want to keep a history of the notifications for users to look at later then you would want to store it in the database. Meaning yes, you would want to create a new table in the DB. You should do this via a migration script. This new model should be associated to the User model so you could do something like user.notifications
to retrieve all their notifications.
Also if you used timestamps
in the new Notifications
model you could do some fun analytics later.
Upvotes: 1
Reputation: 3848
you can use rails polymorphic association for this purpose, using this mechanism you can relate your notification model to other models without modifying target table.
A better example is given in this link http://railscasts.com/episodes/154-polymorphic-association
Upvotes: 0