Im learning ruby on rails and have a trouble with aasm callbacks and actionmailer. I have a hotels model. Heres a code:
class Hotel < ActiveRecord::Base
include AASM
scope :approved_hotels, -> { where(aasm_state: "approved") }
has_many :comments
belongs_to :user, :counter_cache => true
has_many :ratings
belongs_to :address
aasm do
state :pending, initial: true
state :approved
state :rejected
event :approve, :after => :send_email do
transitions from: :pending, to: :approved
end
event :reject, :after => :send_email do
transitions from: :pending, to: :rejected
end
end
def send_email
end
end
As you see user has to get email when state of the hotel he added was changed. Heres what i wrote but its not THE solution cos user gets emails every time admin updates hotel with "pending" state.
class HotelsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show, :top5hotels]
def update
@hotel = Hotel.find(params[:id])
if @hotel.aasm_state == "pending"
@hotel.aasm_state = params[:state]
UserMailer.changed_state_email(current_user, @hotel.name,
@hotel.aasm_state).deliver
end
if @hotel.update_attributes!(params[:hotel])
redirect_to admin_hotel_path(@hotel), notice: "Hotel was successfully updated."
else
render "edit"
end
end
end
So i think i need to use callback but i dont know how to call
UserMailer.changed_state_email(current_user, @hotel.name,
@hotel.aasm_state).deliver
from the model. I tried
UserMailer.changed_state_email(User.find(:id), Hotel.find(:name),
Hotel.find(aasm_state)).deliver
but that doesnt work. Im really out of options and looking for any help. Thanks!