1

當用戶喜歡評論create_notification被觸發在commet_like.rb如何將模型與模型中的模型相關聯?

class CommentLike < ActiveRecord::Base 
    after_create :create_notification 
    has_many :notifications 
    belongs_to :user 
    belongs_to :comment 
    validates :user, uniqueness: { scope: :comment } 
    belongs_to :liker, class_name: 'User', foreign_key: :user_id 
    belongs_to :liked_comment, class_name: 'Comment', foreign_key: :comment_id 

private 

    def create_notification 
    notifications.create(
     comment_like: self, 
     comment:  comment, 
     user:   comment.user,  
     read:   false 
    ) 
    end 
end 

我試圖讓這樣的事情在通知指數工作:

<% if notification.comment_like_id %> 
    <% if notification.goal_id %> 
     liked <%= link_to "your comment", notification_goal_path(notification, notification.goal_id) %> 
    <% elsif notification.habit_id %> 
     liked <%= link_to "your comment", notification_habit_path(notification, notification.habit_id) %> 
    <% elsif notification.quantified_id %> 
     liked <%= link_to "your comment", notification_quantified_path(notification, notification.quantified_id) %> 
    <% elsif notification.valuation_id %> 
     liked <%= link_to "your comment", notification_valuation_path(notification, notification.valuation_id) %> 
    <% end %> 
    <% end %> 

但問題是這些條件中的每一條都不可用於評論,因此通知顯示爲空白。

正如你在這裏雖然可以看到CommentLike與評論相關聯,在本例中給出comment_id: 1comment_id: 1valuation_id: 1

[1] CommentLike.find(1) 
id: 1, 
user_id: 1, 
comment_id: 1, 
[2] Comment.find(1) 
id: 1, 
content: "test", 
goal_id: nil, 
habit_id: nil, 
valuation_id: 1, 
quantified_id: nil, 
commentable_id: nil, 
commentable_type: nil, 
user_id: 1, 
likes: 1> 

我們如何利用這個協會根據意見與其他4款協會做出正確的條件現身comment_like_id下?

notifications_controller

def index 
    @notifications = current_user.notifications 
    @notifications.each do |notification| 
     notification.update_attribute(:read, true) 
    end 
    end 

comment.rb

class Comment < ActiveRecord::Base 
    after_create :create_notification 
    has_many :notifications 
    has_many :comment_likes 
    has_many :likers, through: :comment_likes, class_name: 'User', source: :liker 
    belongs_to :habit 
    belongs_to :quantified 
    belongs_to :valuation 
    belongs_to :goal 
    belongs_to :user 

private 

    def create_notification 
    author = 
     if goal 
     goal.user 
     elsif habit 
     habit.user 
     elsif quantified 
     quantified.user 
     elsif valuation 
     valuation.user 
     end 
    notifications.create(
     comment:  self, 
     habit:  habit, 
     quantified: quantified, 
     goal:   goal, 
     valuation: valuation, 
     user:   author,  
     read:   false 
    ) 
    end 
end 

請讓我知道如果你需要進一步的解釋或代碼: - ]

+0

怎麼來評論通過相關聯的所有其他車型代替commentable(它看起來像你有該領域,但不是協會) – DVG

回答

0

這裏的關鍵是使用after_save :create_notification而不是after_create :create_notification和也likes: likes,添加行notifications.create(。保存因爲在評論控制器工作得更好之後

我有這樣的:

def like 
    @comment_like = current_user.comment_likes.build(comment: @comment) 
    if @comment_like.save 
     @comment.increment!(:likes) 
     flash[:success] = 'Thanks for liking!' 
    else 
     flash[:error] = 'Too many likes' 
    end 
    redirect_to(:back) 
    end 
0

原諒我,因爲我不真的不知道習慣,量化等等是什麼,但是你的代碼使它看起來像他們一樣ngs可以被評論,因此你應該使用多態關聯。

class Comment < ActiveRecord::Base 
    belongs_to :commentable, polymorphic: true 
    has_many :notifications 
    after_create :notify_user 

    def notify_user 
    self.notifications.create user: commentable.user 
    end 
end 

class Habit < ActiveRecord::Base 
    has_many :comments, as: :commentable 
end 

class Notification < ActiveRecord::Base 
    belongs_to :comment 
    belongs_to :user 
    delegate :commentable, to: :comment 
end 

在這裏,通過使用多態關係,我們簡化了一切。另外,您還可以使用多態路徑傭工

<%= link_to "your comment", polymorphic_url(notification, commentable) %> 

希望這可以幫助你指出正確的方向

+0

對不起,這不完全回答這個問題。評論是多態的。這更多關於CommentLikes通過使用polyphorphic註釋找出評價,習慣,目標或量化的評論id的ID –

+0

這將有助於看到您的評論,如模型 – DVG

+0

對不起DVG我添加了上面的完整模型:) –

相關問題