Reputation: 373
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?
Upvotes: 0
Views: 463
Reputation: 30056
Try this one
accepted_terms_titles = @accepted.map { |t| t[:title] }
@pending.reject! { |t| accepted_terms_titles.include?(t[:title]) }
@pending
array the terms whose title is included in the accepted_terms_titles
arrayUpvotes: 1