I'm thinking that notifications would be it's own resource and have a has_many, through relationship with the user model with a join table representing the associations.

A user having many notifications is obvious, and then a notification would have many users because there would be a number of standardized notifications (a commenting notification, a following notification etc.) that would be associated with many users.

Beyond this setup, I'm unsure how to trigger the creation of notifications based on certain events in your application. I'm also a little unsure of how I'd need to set up routing - would it be it's own separate resource or nested in the user resource? I'd find it very helpful if someone could expand on this.

Lastly, ajax polling would likely improve such a feature.

There's probably some things I'm missing, so please fill this out so that it is a good general resource.

推荐答案

So the general gist:

1) Notifications would be a polymorphic association in that comments can have many notifications, users can have many notifications, a 'following' can have many notifications etc.

2) You can have Model Observers, where you can "observe" certain events, such as when a new comment is created. This is would be your triggers.

In terms of routing, you really don't need to do anything out of the norm. The only routing you may have is a domain.com/notifications where it shows all the notifications.

Notification table might look like:

sender_id: integer, receiver_id: integer, notifiable_id: integer, notifiable_type: string

Ruby-on-rails相关问答推荐

Rails 7通过引擎将方法自动加载到控制器中的方法

链轮轨道V4链接到外部Ruby 的 list 文件

是否从已转换为字符串的数组中提取符号?

如何断言 Ruby 单元测试中的错误消息?

CanCanCan、Pagy 和 Meil​​iSearch,如何组合这 3 个 gem?

我如何在不在 gemfile 中的 rake 任务中要求 gem?

Ruby on Rails 中的多种布局

ruby on rails 和 javascript 之间的时间转换反之亦然?

ArgumentError:您需要使用 :if 提供至少一个验证

rails, activerecord, 获取当前连接规范

如何在 routes.rb 中使用 301 或 302 进行重定向

将根 url 重定向到 Rails 应用程序中的其他位置

Rails 3 中的猴子补丁

警告:引用了顶级常量

Ruby 和 Ruby on Rails 离线 API 文档

Ruby on Rails:如何为 postgresql 编辑 database.yml?

Mongoid OR 查询语法

从 Rails 中的 ActiveRecord::RecordNotFound 救援

我应该在 Heroku Cedar 上使用薄的还是独角兽

保存用户偏好的最佳方法?