Zuhaib Ali
Zuhaib Ali

Reputation: 3374

Is it a good practice to save data to a database from within another model? In Rails?

I have an articles and a notifications table. Whenever an article is successfully saved in the database, I send a notification to the user about it from within the model.

My question is...

Is it permissible in the MVC design to do this or should I use controllers for what I'm doing.

Upvotes: 0

Views: 49

Answers (1)

Thilo
Thilo

Reputation: 17735

Absolutely, and you should probably not use a controller for this unless it is exclusively triggered by a request, or dependent on request parameters. In most cases, this is business logic that belongs in the model layer.

Alternatively, you could take a look at observers if you want to keep the actions of saving the article and notifying the user decoupled from each other.

Upvotes: 2

Related Questions