In this episode, we're tackling how to pin discussions in our forum app. Pinning is a key feature because it lets important discussions always appear at the top of the list, no matter how old they are or how much activity other threads are getting.
First up, we modify our database: we add a pinned_at
timestamp column to the discussions table. This way, we can keep track of when a discussion was pinned, and easily know whether any given discussion is currently pinned. We make this column nullable, because most discussions won't be pinned.
Then we jump into our models and resources to add a simple helper method that checks if a discussion is pinned. We also make sure this information gets sent out in our API responses.
On the front end, we just display a basic "Pinned" label next to any pinned discussion. To test it out, we manually pin one of our discussions and see it update.
Lastly, we work on ordering: pinned discussions should always be up top, and the rest will get arranged by their creation date (for now). We add a scope to our discussion model so any list of discussions can easily be ordered with pinned ones first. Eventually, we'll update this to sort by the last post, but for now, this gives us the pinning foundation we need!