ruby on rails - How to associate a model with a model within a model? -
when user likes comment create_notification
triggered in 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
i'm trying work in notifications index:
<% 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 %>
but problem each of conditionals nil comment_like therefore notification showing blank.
as can see here though commentlike associated comment, in example gives comment_id: 1
, comment_id: 1
has 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>
how can use association make right conditional show under comment_like_id
according comment's association 4 other models?
notifications_controller
def index @notifications = current_user.notifications @notifications.each |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
please let me know if need further explanation or code :-]
forgive me, since don't know habit, quantified, etc are, code makes things commented on, therefore seems should using polymorphic association.
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
here, using polymorphic relationship, simplify quite bit. further, can use polymorphic route helper
<%= link_to "your comment", polymorphic_url(notification, commentable) %>
hope helps point in right direction
Comments
Post a Comment