Can anyone give me some help with a design please? my users enter short generic updates on my site, if they enter the same update more than twice in a certain time limit, the update will be available as a “quick link” so in future they can enter the same update quickly.
What’s the best way to model this please? 2 tables, one for the updates, and another that maintains updates that have been entered more than once, and have it sorted by frequency. If I have this system, whenever a new update is added, I will have to scan 2 tables to see if it has already been entered before; not ideal.
Assuming you’re using Active Record you can use the find_or_create_by method.
Let’s say you have a table as follows:
Message
id: int
body: String
user_id: int
created_at: DateTime
If this table is in place you can do the following:
This will either find the message with body “string” or create a new one for you.
If you also need to update the frequency you can add this to the message table and call: