0

I have a Term model which includes attributes :title (which is simply a title), as well as :accepted, and :pending (which are boolean values to tell if a Term has been approved by a moderator). On my index page, the Terms are split up by their :accepted attribute and stored in arrays called @accepted and @pending.

There can be multiple Terms with the same :title, however, only one of these can be accepted.

What I am unsuccessfully trying to do is filter the two arrays by the :title attribute so that if a term appears in the @approved array, it doesn't appear in the @pending array.

So if I have:

@accepted = [{title: "TERM1", accepted: true, pending: false}]
@pending  = [{title: "TERM1", accepted: false, pending: true}, {title: "TERM2", accepted: false, pending: true}, {title: "TERM2", accepted: false , pending: true}]

After filtering, @pending will contain only the second and third objects (i.e. all that do not have the title "TERM1").

How would I perform this filter?

1
  • Since you only want each term title to be unique why don't you deal with this at the time of creation with a uniqueness validation on title? You can even supply your users with a message that the term is already accepted or pending for approval. Seems messy to keep multiple records you don't need and then have them lying around and not display them to admins so it looks cleaner. Commented Mar 19, 2019 at 22:18

1 Answer 1

1

Try this one

accepted_terms_titles = @accepted.map { |t| t[:title] }
@pending.reject! { |t| accepted_terms_titles.include?(t[:title]) }
  1. Collect all the titles for accepted terms
  2. Filter out in the @pending array the terms whose title is included in the accepted_terms_titles array
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.