Help selecting the correct elements in an array

I’m working on a method but the results are “the opposite” of what I
want to happen. Here’s what I have in my model:

after_update :foo

def foo
self.scores.where(:standard_id => self.standard_ids).each do |
score|
score.destroy
end
end

… which actually deletes the scores I want to keep. What I want to
do is something like:

self.scores - self.scores.where(:standard_id =>
self.standard_ids).each do…

OR

a = self.scores
b = self.scores.where(:standard_id => self.standard_ids)
a - b = c
c.each do…

OR

self.scores.reject(self.scores.where(:standard_id =>
self.standard_ids)).each do…

Any help is greatly appreciated! Thanks in advance :slight_smile:

On Fri, Nov 5, 2010 at 6:20 AM, DemetriusOlsen [email protected]
wrote:

I’m working on a method but the results are “the opposite” of what I
want to happen. Here’s what I have in my model:

Any help is greatly appreciated! Thanks in advance :slight_smile:

If an extra query is not too expensive for your situation, how about:

scores.where([“standard_id NOT IN (?)”, standard_ids])

Do you really need self in those statements?

Hope that helps,
Ammar

Ammar,

If an extra query is not too expensive for your situation, how about:

scores.where([“standard_id NOT IN (?)”, standard_ids])

This worked! Thank you! At this point, I’m glad it worked and have no
idea how to tell if it is “too expensive.” Can you recommend a
resource that provides more info?

Do you really need self in those statements?
Just searched for more info about when to use self (or not) and came
up with this advice: “Never use self for retrieving values, since its
unnecessary and adds bulk. …in the context of ActiveRecord.”
http://bit.ly/cVgYK This was helpful too:
http://paulbarry.com/articles/2008/04/17/the-rules-of-ruby-self

Thanks again for your help. :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs