如何将模型与模型中的模型相关联?

How to associate a model with a model within a model?

当用户点赞评论时 create_notificationcommet_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 %>

但问题是每个条件对于 comment_like 都是 nil,因此通知显示为空白。

正如您在此处看到的,尽管 CommentLike 与 Comment 相关联,在此示例中给出 comment_id: 1 并且 comment_id: 1 具有 valuation_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

如果您需要进一步的解释或代码,请告诉我:-]

请原谅,因为我真的不知道习惯、量化等是什么,但是你的代码让它看起来像是可以评论的东西,因此看起来你应该使用多态关联.

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) %>

希望这能帮助您指明正确的方向

此处的关键是使用 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