David Kimura PRO
Joined 7/18/2015
Drifting Ruby Owner
Ruby Rogues Panelist
David Kimura PRO said about 4 years ago on ActiveRecord Tricks :
  add a bang to User.create

User.create! do |user|
 ...
end

This won't fix the problem, but will fail and you can see what the error is.

David Kimura PRO said about 4 years ago on ActiveRecord Tricks :
You probably have some other association on the user (belongs_to) which is causing the user to not save.

David Kimura PRO said almost 4 years ago on User Notifications :
  Can you post your Ruby class for the CommentNotifier?

David Kimura PRO said almost 4 years ago on User Notifications :
   You can put it in your app/models or create a new folder in app like services and put it in there.


David Kimura PRO said almost 4 years ago on User Notifications :
  The CommentNotifier would be it's own class. 

# app/models/comment_notifier.rb
class CommentNotifier
  # this is a class method, so it will be called like CommentNotifier.call(@comment, current_user) from your controller
  def self.call(commnet, current_user)
    new.call(comment, current_user)
  end

  # initialization stuff for the CommentNotifier instance
  attr_accessor :comment, :current_user
  def initialize(comment, current_user)
    @comment = comment
    @current_user = current_user
  end

  # this is your main method where the business logic will happen.
  def call
    Rails.logger.info comment
    Rails.logger.info current_user
    # thanks to attr_accessor, I can simple reference to commant and current_user without the @comment or @current_user

    # example guard clauses
    return unless current_user
    return unless comment
    return unless user_same_as_commenter

    # do other actions and checks
  end

  private

  # because we create a methods within this class, we can call on these methods since we initialized a new object in the (new.call(comment, current_user))
  def user_same_as_commenter
    current_user == comment.user
  end
end