Monday, 15 July 2013

model - Using conditionals on callbacks rails -



model - Using conditionals on callbacks rails -

i have callback on comment model creates notification gets sent out appropriate members don't want create notification if current_member commenting on own commentable object. i've tried using unless conditional this:

after_create :create_notification, on: :create, unless: proc.new { |commentable| commentable.member == current_member } def create_notification subject = "#{member.user_name}" body = "wrote <b>comment</b> <p><i>#{content}</i></p>" commentable.member.notify(subject, body, self) end

but error: undefined local variable or method 'current_member' #<comment:0x746e008

how work want?

it's pretty atypical seek utilize current_user or things model layer. 1 problem you're coupling model layer current state of controller layer, create unit testing models much more hard , error-prone.

what recommend not utilize after_create hook this, , instead create notifications @ controller layer. give access current_user without needing jump through hoops.

ruby-on-rails model callback conditional

No comments:

Post a Comment