0

I'm using rails mailboxer to handle my app's notifications. I've been able to send a notification to single users as outlined in their documentation but I can't figure out how to send a notification to an array of users. In my case, their followers.

I get this error when I try to send to an array:

undefined method `notify_all' for #<Array:0x696faf8>

My Model:

class Update < ActiveRecord::Base
    belongs_to :member
    belongs_to :updateable, polymorphic: true
    attr_accessible :title, :content

    after_create :create_notification, on: :create

    def create_notification
    subject = "#{member.user_name}"
    body = "posted a new update <b>#{title}:</b> <p><i>#{content}</i></p>"
    updateable.followers.notify_all(subject, body, self)
    end
end

My Controller:

class UpdatesController < ApplicationController
    before_filter :authenticate_member!
    before_filter :load_updateable
    before_filter :find_member

    def index
        redirect_to root_path
    end

    def new
        @update = @updateable.updates.new
    end

    def create
        @update = @updateable.updates.new(params[:update])
        @update.member = current_member

        respond_to do |format|
        if @update.save
            format.html { redirect_to @updateable }
            format.json { render json: @updateable }
        else
            format.html { redirect_to @updateable }
            format.json { render json: @updateable.errors, status: :unprocessable_entity }
        end
    end 
end

def destroy
    @update = Update.find(params[:id])

    respond_to do |format|
      if @update.member == current_member || @updateable.member == current_member
        @update.destroy
        format.html { redirect_to :back }
      else
        format.html { redirect_to :back, alert: 'You can\'t delete this update.' }
      end
    end 
end

private

# def load_updateable
#       resource, id = request.path.split('/')[1,2] # photos/1/
#       @updateable = resource.singularize.classify.constantize.find(id) #    Photo.find(1)
# end 

# alternative option:
def load_updateable
    klass = [Project, Event].detect { |c| params["#{c.name.underscore}_id"] }
    @updateable = klass.find(params["#{klass.name.underscore}_id"])
end

def find_member
    @member = Member.find_by_user_name(params[:user_name])
end 

end
iamdhunt
  • 443
  • 6
  • 16

2 Answers2

1

As highlighted by jsksma2 you could change your model to be:

class Update < ActiveRecord::Base
  belongs_to :member
  belongs_to :updateable, polymorphic: true
  attr_accessible :title, :content

  after_create :create_notification, on: :create

  def create_notification
    subject = "#{member.user_name}"
    body = "posted a new update <b>#{title}:</b> <p><i>#{content}</i></p>"
    updateable.followers.each { |follower| follower.notify_all(subject, body, self) }
  end
end
Cristiano Mendonça
  • 1,220
  • 1
  • 10
  • 21
  • Thanks this what I was looking for. Only edit would be with this I only need to call .notify instead of .notify_all – iamdhunt Jun 19 '14 at 23:39
0

If you read the error, it clearly states your problem: You're trying to call a method on an array. The array class doesn't know what notify_all is so it's undefined.

Your immediate solution would be something like:

array.each do |obj|
  obj.notify_all
end

Assuming that the objects contained inside the array are the correct class (they contain the method).

Judging by the naming convention of your method, I'm going to guess that it's designed to handle an array; by which I would suggest refactoring your method to be more like:

Class.notify_all(array)
jakenberg
  • 2,125
  • 20
  • 38