質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

1932閲覧

DB設計(Model定義)ミスの修正について

lapi

総合スコア58

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2017/03/14 06:35

###前提・実現したいこと
通知機能の実装をしています。
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

現在以上のようになっているのですが、どのように修正したらよろしいのでしょうか?

ご教授よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Rails5なら Action Cableを利用して通知機能が簡単に実装できるのですが
Rails4でPusherを利用しているのですね。少し読み込みが足りませんでした、すみません

そうすると、下手にリファクタリングすると、修正箇所が膨大になりそうなので、前回の質問の回答を修正する形でいきましょう。

投稿2017/03/14 07:08

moke

総合スコア2241

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

lapi

2017/03/14 13:34

通知機能を実装する際に色々調べてみてAction Cableを見つけましたが、今回Rails4で開発していたのでPusherを利用して実装していました。 畏まりました。 それではmokeさんの仰るように前回の質問の回答を修正する形でやりたいと思います。 前回mokeさんが修正していただいた <% @notifications.each do |notification| %> <%= (content_tag :p ,"#{notification.comment.user.try(:name)}さんが あなたの投稿#{<%= (link_to notification.comment.topic.title, topic_path(notification.comment.topic, notification_id: notification.id)})にコメントしました。") if notification.comment.present? %> <%= (content_tag :p ,"#{notification.commenttl.user.try(:name)}さんが あなたの投稿(#{<%= (link_to notification.commenttl.timeline.content, timeline_path(notification.commenttl.timeline, notification_id: notification.id)})にコメントしました。") if notification.commenttl.present? %> <% end %> こちらなんですが、 syntax error, unexpected '<' syntax error, unexpected keyword_end, expecting tSTRING_DEND syntax error, unexpected keyword_ensure, expecting tSTRING_DEND syntax error, unexpected keyword_end, expecting tSTRING_DEND とエラーが出てきてしまい表示できませんでした。
lapi

2017/03/14 14:22

最初に教えて頂いた <% @notifications.each do |notification| %> <p> <%= notification.comment.user.try(:name) if notification.comment.present? %>さんが あなたの投稿(<%= (link_to "#{notification.comment.topic.title}", topic_path(notification.comment.topic, notification_id: notification.id)) if notification.comment.present? %>)にコメントしました。 </p> <p> <%= notification.commenttl.user.try(:name) if notification.commenttl.present? %>さんが あなたの投稿(<%= (link_to "#{notification.commenttl.timeline.content}", timeline_path(notification.commenttl.timeline, notification_id: notification.id)) if notification.commenttl.present? %>)にコメントしました。 </p> <% end %> こちらでは正常に表示できます。 ですが、 hogeさんが あなたの投稿(huga)にコメントしました。 さんが あなたの投稿()にコメントしました。 と、このように、片方のお知らせが空のまま表示されてしまいます。 このような場合、どのように修正したら良いのでしょうか?
moke

2017/03/14 14:47

<% @notifications.each do |notification| %> <%= (content_tag :p ,"#{notification.comment.user.try(:name)}さんが あなたの投稿#{(link_to notification.comment.topic.title, topic_path(notification.comment.topic, notification_id: notification.id)})にコメントしました。") if notification.comment.present? %> <%= (content_tag :p ,"#{notification.commenttl.user.try(:name)}さんが あなたの投稿(#{(link_to notification.commenttl.timeline.content, timeline_path(notification.commenttl.timeline, notification_id: notification.id)})にコメントしました。") if notification.commenttl.present? %> <% end %>
moke

2017/03/14 14:48

こうでしょうね向こうも直しときます
lapi

2017/03/14 16:02 編集

ありがとうございます。 教えて頂いたように修正してみたところ syntax error, unexpected tSTRING_DEND, expecting ')' unterminated string meets end of file syntax error, unexpected end-of-input, expecting ')' このようにエラーが新たに出てしまいました。 なんとか自力で解決しようと試みてみましたが、エラーは解消できませんでした。 どのように修正すべきなんでしょうか?
moke

2017/03/15 04:03 編集

)が一つ多かったようです。すみません <% @notifications.each do |notification| %> <%= (content_tag :p ,"#{notification.comment.user.try(:name)}さんがあなたの投稿#{link_to notification.comment.topic.title, topic_path(notification.comment.topic, notification_id: notification.id)}にコメントしました。") if notification.comment.present? %> <%= (content_tag :p ,"#{notification.commenttl.user.try(:name)}さんがあなたの投稿#{link_to notification.commenttl.timeline.content, timeline_path(notification.commenttl.timeline, notification_id: notification.id)}にコメントしました。") if notification.commenttl.present? %> <% end %>
lapi

2017/03/15 16:49

ありがとうございます。 mokeさんの言う通り修正したら無事に表示できました。 ですが、後1点だけ解決できない部分があって、 mokeさんにご教授頂いたように記述するとリンクが <a href="/timelines/5?notification_id=45">投稿title</a> このように表示されてしまいリンクが生成されません。 これは何が原因なんでしょうか?
moke

2017/03/17 02:21

.html_safe と末尾(ifの前につけて下さい)
lapi

2017/03/17 03:31

ありがとうございました。 html_safeメソッドを使用したら解決できました。 まだまだ自分の知識量が少なくて分からないことが多いので、今後さらに頑張っていこうと思います。 ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問