...

/

Storing Support Requests from Our Mailbox

Storing Support Requests from Our Mailbox

Learn to store support requests from our mailbox using the models.

As we mentioned above, the purpose of mailboxes is to allow us to execute code on every email we receive. Since emails come in whenever the sender sends them, we’ll need to store the details of a customer support request somewhere for an administrator to handle later. To that end, we’ll create a new model called SupportRequest that will hold the relevant details of the request. We’ll also have the process() method of SupportMailbox create an instance for each email we get (in the final section of next lesson we’ll display these in a UI so an admin can respond).

Creating a model for support requests

We want our model to hold the sender’s email, the subject and body of the email, and a reference to the customer’s most recent order if there’s one on file. First, let’s create the model using a Rails generator:

bin/rails g model support_request
Terminal 1
Terminal
Loading...

This created a migration for us, which is currently empty. Remember that migration filenames have a date and time in them, so your filename will be slightly different. Let’s fill the migration in.

Press + to interact
class CreateSupportRequests < ActiveRecord::Migration[6.0]
def change
create_table :support_requests do |t|
t.string :email, comment: "Email of the submitter"
t.string :subject, comment: "Subject of their support email"
t.text :body, comment: "Body of their support email"
t.references :order,
foreign_key: true,
comment: "their most recent order, if applicable"
t.timestamps
end
end
end

With this in place, we can create this table via bin/rails db:migrate:

bin/rails db:migrate
...