So now that we have our first relationship, that means we'll never touch it again, right? Nope. In fact, we are going to mess with that code yet again to further improve the style of code to make more it "Rubyesque" in style and convention as well as easier to read. But first, we'll knock another relationship out of the park.
The next relationship between content that we'll work on is probably the easiest to implement. We are going to grab the most recent piece of content from the current author and obtain the permlink so we can later display that new content as one of the options we give to users.
We are going to use the Radiator API in the same way that we did for the previous relationship except we will adjust our filters such that we look for comments instead of votes and such that we look for comments with no parent authors (and thus making the piece of content a post rather than a secondary comment). The final result is shown below:
You notice that there are a view differences in how the code is formatted even though we are doing essentially the same thing as we did in the previous relationship except for votes. First off, we use multiple closures this time to loop through and into the hash. This means that we can grab the pieces we need without log chains of indices and fields called on top of each other. This way of doing things is cleaner and easier to read while doing the same thing.
Another change you might notice as that we have added a 'Z' to the timestamp. This simply converts our timestamp to UTC time such that it matches the timezone that the Time object and most computers in general natively use.
The last change is inside our filter. Rather than chain together a long conditional statement, we split the statement into the three parts and only allow the process through if passes (or in this case fails) each test. Next essentially jumps to the next iteration of the loop. So if the conditional is true, we skip remaining computation, but if it is false, then we do not skip. The unless keyword flips the next conditional for those new to Ruby. We skip computation unless the thing is true.
But other than these stylistic changes and the small adjustments to account for the relationship, the code does pretty much the same thing as the old code for the first relationship. But for reference, the first relationship is redone using the style that we covered today. Next time, we'll take a break from the relationships and begin organizing our new code in the dormant Rails project.