When do you not need new, show, create, edit, update and delete actions
The first time I learnt that not every Controller needs every action
I was trying to build my rails application that matches users with each other every week based on their preferences. This app had a User database - to store user info and preferences - and a Matches database - to store details of all past matches. Here’s a simplified version of what’s happening in the application
User signs up through a form that asks them for their preferences about who they would like to meet. They also fill out details about themselves. This happens in the Users Controller.
Then, based on every user’s preferences, the application would find the best match for each user.
I asked ChatGPT for a blueprint of the controllers. It gave me a blueprint for the Matches Controller but it looked peculiar - it did not contain show, new, edit and update actions!
Being a new Rails developer, I had never seen this before. I thought this was a mistake. So, I asked it.
No need for show, new, edit and update actions
Me: Don’t I need the show, new, edit and update actions in the Matches controller?
ChatGPT: Based on your initial description of the platform, it seems that you don't need the show, new, edit, and update actions in the Matches controller. The main purpose of the platform is to match users with other likeminded members, and this can be accomplished using the index action to display the matches and the create action to generate new matches.
Here's a simplified version of the Matches controller without the unnecessary actions:
class MatchesController < ApplicationController
before_action :set_user, only: [:index]
# GET /matches
def index
@matches = @user.matches
end
# POST /matches
def create
# Your matching logic goes here
# Create a new match and save it to the database
if @match.save
# Redirect or render the appropriate view, depending on your requirements
else
# Handle the error case
end
end
private
def set_user
@user = User.find(params[:user_id])
end
end
This controller should suffice for your use case. However, if you later decide to add more functionality to the platform, such as allowing users to view details of a specific match or edit their matches, you may need to add the corresponding actions back to the controller.
When you don’t even need the create action
As I progressed along building the application, ChatGPT told me that I will need to create a Background Job in order to create the matches automatically each week. This would require me to create a MatchUsersJob
class that will be responsible for creating new matches and saving it to the Matches database.
But this was contrary to what it had suggested above - when it asked me to implement matching logic in the create action. So, I asked -
Me: But the create action of the Matches controller is also responsible for creating a new match object and saving it to the database. Is that correct?
ChatGPT: Yes, you are correct. The create
action in the MatchesController
is also responsible for creating a new Match object and saving it to the database. However, in the context of the matching process we discussed, the actual creation and saving of the Match record is handled by the MatchUsersJob
class, not by the MatchesController's create action directly.
The reason for this distinction is that the matching process should be performed asynchronously in the background, using a job instead of being done directly within the controller's action. This allows the application to perform matching tasks without blocking the main application flow or causing a delay in the user's experience.
When you use the MatchUsersJob
, it's the job that will call the find_user_to_match()
method (which is now in the User model) and create the match record using Match.create
. The create action of the MatchesController would not be directly involved in this process in this specific implementation. The controller's main responsibility would be to initiate the job when needed.
Me: So do you mean to say that we don't need the create action of the MatchesController?
ChatGPT: You may still need the create action in the MatchesController
for other purposes, such as creating a new match record through a different process or a manual intervention by an administrator. In such cases, the create action would be used to create and save the match record in the traditional way, using the parameters submitted from a form, for example.
If you are sure that you will only create matches through the MatchUsersJob
and the background matching process, then you could potentially remove the create action from the MatchesController
. But, in general, it's a good idea to have it available in case you need it for other purposes or future expansions of your application.