Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Help selecting the correct elements in an array

Reply
Thread Tools

Help selecting the correct elements in an array

 
 
DemetriusOlsen
Guest
Posts: n/a
 
      11-05-2010
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
 
Reply With Quote
 
 
 
 
Ammar Ali
Guest
Posts: n/a
 
      11-05-2010
On Fri, Nov 5, 2010 at 6:20 AM, DemetriusOlsen <(E-Mail Removed)> 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:
>


<snip>

> 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

 
Reply With Quote
 
 
 
 
DemetriusOlsen
Guest
Posts: n/a
 
      11-05-2010
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/0...s-of-ruby-self

Thanks again for your help.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath selecting elements afer one having a given text =?ISO-8859-1?Q?Une_B=E9vue?= XML 1 02-22-2007 01:29 PM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
Wireless Router - Selecting the correct one =?Utf-8?B?THlubmV0dGU=?= Wireless Networking 0 04-18-2006 03:44 AM
Selecting elements from a list Martin Christensen Python 9 09-05-2003 08:20 PM
finding and selecting the correct value with a databound dropdownlist Matt ASP .Net Web Controls 0 07-09-2003 06:59 PM



Advertisments