Right now I’m working with a team of developers on the ReadThisNext mobile app – they are working in Swift, and I am building the Rails API. That means every week or so they send a new build, and ask me to fix things that are broken with the API. Recently they ask why the “Create Post” action was not working.
After some sleuthing, the error messages were telling me that Create Post was triggering Create Book which was triggering Soulmate (the plugin that adds books to search results). The problem is, when you create a post with a book_id included, this should not happen. Create Post should only create a book if book_id is missing.
This was all through the curl command, which I use to test the endpoints I send to developers.
I went to check the same action through the web interface. Oddly enough, it was still hitting Soulmate, because it gave me an error unless Redis (works with Soulmate) was running.
I tried removing the command that would add a new book if book_id was empty and still got an error in curl saying book_id was empty because the validation is set to prevent a post from being created without a book. On the web, I could add a post with the correct book successfully.
It seemed like curl wasn’t recognizing that I had included the book_id. I tried changing the validations to after_create or after_validation, just in case the Rails validations were only seeing text in a text box and not in the curl command for some reason. That didn’t work.
I tried searching for this problem and came up with nothing. My general rule with programming is that, if you have a problem, it’s likely that someone else has had it before. When I can’t find it with a simple Google search, something is fishy.
Because it was working on web, I knew it had to be something to do with the curl command, so I asked a question on Stack Overflow. The first comment was: “’twould be helpful to include the controller code that you hit with that POST.” I wish people on Stack Overflow didn’t have to sound like such dicks when they ask obvious questions.
But, as I went to find the Create Post code which he was referring to, I decided to include another bit that tells Rails which attributes it is allowed to update. I thought I had checked this before, but I hadn’t – because book_id was missing. I must have checked the code for web, which is on a different page.
After 1+ hours of working on this solo, it saved me a ton of time to post it on Stack Overflow, even if someone else didn’t technically answer my question. Otherwise I might have given up and removed a feature, or tried to build around it, when the answer was really very simple!