简体   繁体   中英

Allowing users to hide submissions in Rails 3

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:

  1. Submission
  2. Hidden
  3. User

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

Unless I missed something, I'd simply add a 'hidden' attribute to the Submission model (Boolean).

When the user clicks your "hide" link, you set the attribute to true.

Then you simply look for submissions with the flag set to false:

Submission.where(:hidden => false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM