###前提・実現したいこと
通知機能の実装をしています。
2つのお知らせを受け取り、そのお知らせを一覧ページで表示させようと思っています。
具体的には、
・トピックにて投稿した記事にコメントがついたらお知らせ
・タイムラインに投稿したものにコメントがついたらお知らせ
このようにしたいと思っていました。
###発生している問題・エラーメッセージ
一通りやってみたのですが、DB設計(Model定義)ミスで一覧ページで上手に表示することができません。
具体的には、
データの取得方法、eachメソッドについて
こちらで質問させていただいた内容です。
###該当のソースコード
models/topic.rb
class Topic < ActiveRecord::Base validates :title, presence: true validates :content, presence: true # Topicモデルのレコードがuserモデルのレコードに属することを定義 belongs_to :user # CommentモデルのAssociationを設定 has_many :comments, dependent: :destroy end
models/comment.rb
[トピックに対するコメント]
class Comment < ActiveRecord::Base belongs_to :user belongs_to :topic has_many :notifications, dependent: :destroy end
models/timeline.rb
class Timeline < ActiveRecord::Base validates :content, presence: true # Timelineモデルのレコードがuserモデルのレコードに属することを定義 belongs_to :user # CommenttlモデルのAssociationを設定 has_many :commenttls, dependent: :destroy end
models/commenttl.rb
[タイムラインに対するコメント]
class Commenttl < ActiveRecord::Base belongs_to :user belongs_to :timeline has_many :notifications, dependent: :destroy end
models/notification.rb
[お知らせのためのモデル]
class Notification < ActiveRecord::Base belongs_to :user belongs_to :comment belongs_to :commenttl end
データベース
create_table "comments", force: :cascade do |t| t.integer "user_id" t.integer "topic_id" t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "comments", ["topic_id"], name: "index_comments_on_topic_id", using: :btree add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree create_table "commenttls", force: :cascade do |t| t.integer "user_id" t.integer "timeline_id" t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false end add_index "commenttls", ["timeline_id"], name: "index_commenttls_on_timeline_id", using: :btree add_index "commenttls", ["user_id"], name: "index_commenttls_on_user_id", using: :btree create_table "notifications", force: :cascade do |t| t.boolean "read", default: false t.integer "user_id" t.integer "comment_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "commenttl_id" end add_index "notifications", ["comment_id"], name: "index_notifications_on_comment_id", using: :btree add_index "notifications", ["commenttl_id"], name: "index_notifications_on_commenttl_id", using: :btree add_index "notifications", ["user_id"], name: "index_notifications_on_user_id", using: :btree create_table "timelines", force: :cascade do |t| t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" end create_table "topics", force: :cascade do |t| t.string "title" t.text "content" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.integer "user_id" end create_table "users", force: :cascade do |t| 省略 end add_foreign_key "comments", "topics" add_foreign_key "comments", "users" add_foreign_key "commenttls", "timelines" add_foreign_key "commenttls", "users" add_foreign_key "notifications", "comments" add_foreign_key "notifications", "commenttls" add_foreign_key "notifications", "users" end
現在以上のようになっているのですが、どのように修正したらよろしいのでしょうか?
ご教授よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/14 13:34
2017/03/14 14:22
2017/03/14 14:47
2017/03/14 14:48
2017/03/14 16:02 編集
2017/03/15 04:03 編集
2017/03/15 16:49
2017/03/17 02:21
2017/03/17 03:31