Reputation: 827
Forgive a basic question here.
From reading around, I understand it's not best practice to trigger the create action of a controller from another - I'm looking for advice on how I should be organizing my code in the following situation:
I have two controllers: cases and notifications.
When a new case is created I want it to also create a new notification.
The notification create action creates a new instance in it's model, and sends an email.
Am I right to think that I shouldn't just be calling Notification#create from my cases controller? Should I be extracting this to a helper or module? If so, what would it look like? The other roughly similar posts on this topic don't elaborate.
Thank you.
Upvotes: 3
Views: 405
Reputation: 11929
As one of options you can use rails observers for creating notification http://api.rubyonrails.org/classes/ActiveRecord/Observer.html
There are few differences between observers and callbacks, for example observers can't cancel any model action, but in your case I think there is no matter what to use.
But observers also used to adhere to the Single Responsibility principle.
Example
class CaseObserver < ActiveRecord::Observer
def after_create(case)
#create notification
end
end
should be saved to /app/models/case_observer.rb.
in your config/application.rb
config.active_record.observers = :case_observer
Upvotes: 1
Reputation: 2945
This logic should be in your models. Good solution is to use ActiveRecord callbacks for it.
# app/models/case.rb
class Case < ActiveRecord:Base
after_create :create_notification
private # <--- bottom of your model
def create_notification
Notification.create!(some_options_for_notification)
end
end
# app/models/notification.rb
class Notification < ActiveRecord:Base
after_create :send_notification
def send(opts)
... # <--- some logic to send notification here
end
private # <--- bottom of your model
def send_notification
send(some_options_for_sending)
end
end
Upvotes: 1
Reputation: 71
Since create is a POST method, I don't think there is a way for calling create of notifications controller from cases controller. My suggestion would be to move the creation of notification instance and the sending mail logic to the before save of your case model. This would create a notification each time a case is created and would also take care of the mail sending mechanism. Since this is a part of your business requirement it is better to move the business logic into your model.
Upvotes: 3
Reputation: 8065
You just need to write Notification.create in your case controllers action, that's it.
You can write as much of code, create as much of model in your controller action as much you want.
Upvotes: 0