【问题标题】:Rails notifications table issueRails 通知表问题
【发布时间】:2011-10-08 19:19:51
【问题描述】:

我目前在 Rails 中有一个通知表,其中包含以下列:“notification_type”、“notification_id”

通知类型为“发布”、“朋友”、“评分”。我有单独的表,名称与通知类型相同。

我想要做的是能够使用 notification_type 和 ID 从其他表中获取正确的数据。即,notification_type 为“Post”且 ID 为 1 的行应该从 ID 为 1 的 Post 表中获取数据。(但是,如果有更多行,我希望从 Post 中获取不止一行“发布”通知类型)。

我还需要将从三个表中检索到的所有数据组合在一起,这样我就可以得到类似的东西:

@all_notifications = ...

在我看来这样称呼:

@all_notifications.each do |notification|
    if notification.type == "Post"
        <%= notification.post_content %>
    end
end

或者,我愿意就如何组织我的表格提出更好的建议。

提前致谢

【问题讨论】:

    标签: sql ruby-on-rails ruby database


    【解决方案1】:

    到目前为止,您似乎想要polymorphic associations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      相关资源
      最近更新 更多