I’ve got a site where users submit submissions and they all get displayed on the homepage. I want to put a little button on each submission that says “hide”…If a user clicks this link they will no longer see the submission anywhere on the site.
Currently my index action for the submission is simply:
def index
@submissions = Submission.order("created_at DESC")
end
My plan was to create a model hidden like so:
rails g model hidden user_id:integer submission_id:integer
Then, a hidden model instance gets created when the user hides something. I would then have to update my controller to something like:
def index
@submissions = Submission.order("created_at DESC").includes("hidden").where("hiddens.user_id IS NULL")
end
Though this obviously won’t work.
Do I have a good database schema for this? What is the most efficient query I can do to make sure I don’t display hidden submissions?
Thanks!
You should have 3 tables:
note you have many-to-many associations. Now whenever user clicks button to hide Submission, you create a row in table Hidden with proper submission_id and user_id. Then, in your controller, you can fetch all Submissions without ids that user marked as “hidden”.
By the way: be aware that you should implement reverse from hiding (in case someone made a mistake or changed his mind0