Help selecting the correct elements in an array

D

DemetriusOlsen

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 :)
 
A

Ammar Ali

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 :)

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
 
D

DemetriusOlsen

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 it’s
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top