Reputation: 4730
So I have two models:
class ModelA < ActiveRecord::Base
has_many :model_b
end
class ModelB < ActiveRecord::Base
belongs_to :model_a
end
When ModelA is created, the controller sends an email:
if @model_aA.save
Emailer.delay.new_model_a(@model_a.id)
And if it's edited:
if @model_a.update(model_a_params)
Emailer.delay.edit_model_a(@model_a.id)
However, if the edit is the creation of a modelB object, the edit email isn't sent. It sends if a modelB owned by modelA is edited, but not if it is created.
How would I change this behaviour?
Upvotes: 0
Views: 198
Reputation: 3437
In general you can use model callbacks for that. You don't need to call the mailer methods on the controller but in an after_create/after_save/after_update hook for example.
http://apidock.com/rails/ActiveRecord/Callbacks/after_save
class ModelA < ActiveRecord::Base
after_create { |instance| Emailer.delay.new_model_a(instance.id) }
after_update { |instance| Emailer.delay.edit_model_a(instance.id) }
end
class ModelB < ActiveRecord::Base
after_create { |instance| Emailer.delay.new_model_b(instance.id) }
end
Upvotes: 1