In this episode, we tackle the challenge of seeding our database with a bunch of fake messages so we can properly test scrolling up and loading previous messages in our chat application. This is all about setting up the environment so we aren't pushing too much data to the page at once, but still see how things behave with a realistic number of messages.
We start off by building out the Message
model and its migration, making sure that each message is connected to both a user and a room with the appropriate foreign keys. Each message just has a simple text body, along with those associations.
Even though we have a factory set up, we focus on writing a custom seeder so we can control the creation order and timestamps. The key here is to create messages in a specific sequence, so their created_at
timestamps are an hour (or a minute) apart, simulating a real conversation. We use a sequence with the factory to slightly change the body of each message (like "message 1", "message 2", etc.) and carefully set the timestamps by starting a year in the past and adding an hour for each message.
Finally, we run the seeder and check the results in the database, confirming that our fake messages now have incrementally increasing timestamps. This will help us later when we implement message loading and ordering. In the end, you’ll have a realistic dataset that makes your chat interface much more testable!