Help selecting the correct elements in an array

Discussion in 'Ruby' started by DemetriusOlsen, Nov 5, 2010.

  1. 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 :)
    DemetriusOlsen, Nov 5, 2010
    #1
    1. Advertising

  2. DemetriusOlsen

    Ammar Ali Guest

    On Fri, Nov 5, 2010 at 6:20 AM, DemetriusOlsen <> 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
    Ammar Ali, Nov 5, 2010
    #2
    1. Advertising

  3. 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. :)
    DemetriusOlsen, Nov 5, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. P
    Replies:
    1
    Views:
    1,161
    Joe Kesselman
    Jul 7, 2006
  2. Martin Christensen

    Selecting elements from a list

    Martin Christensen, Sep 4, 2003, in forum: Python
    Replies:
    9
    Views:
    311
    Martin Christensen
    Sep 5, 2003
  3. =?ISO-8859-1?Q?Une_B=E9vue?=

    XPath selecting elements afer one having a given text

    =?ISO-8859-1?Q?Une_B=E9vue?=, Feb 22, 2007, in forum: XML
    Replies:
    1
    Views:
    757
    =?ISO-8859-1?Q?Une_B=E9vue?=
    Feb 22, 2007
  4. Thomas Schmidt
    Replies:
    7
    Views:
    1,126
    Thomas Schmidt
    Jul 19, 2007
  5. Matt
    Replies:
    0
    Views:
    100
Loading...

Share This Page