[OT maybe] ActiveRecord curious behaviour

Discussion in 'Ruby' started by Alexandru E. Ungur, Jan 25, 2007.

  1. Hi all,

    I appologise if this question is offtopic, hopefully it isn't though...

    I have:
    Forum has_many :comments

    so obviously, I access the comments collection as
    some_forum.comments

    check its type with:
    some_forum.comments.class # => Array

    ok, that I expected, so I want to use Array#find on it, right?
    some_forum.comments.find {...}

    but oops, I get:
    ActiveRecord::RecordNotFound: Couldn't find Comment without an ID

    Hmm, that find is not the find I expected to find ;-) Looks like
    an AR#find to me... but on an Array? It's not like I need SQL to
    find certain elements of an Array... so I find this behaviour very
    interesting. Am I doing something wrong here ? I can find the
    element(s) I want from it, by iterating over it and selecting them,
    but not by using find.

    BTW, it's Rails 1.1.6/AR 1.14.4 I'm having this behaviour on, haven't
    tested it on other versions.


    Thank you in advance,
    Alex
     
    Alexandru E. Ungur, Jan 25, 2007
    #1
    1. Advertising

  2. Hi Alex

    On 1/25/07, Alexandru E. Ungur <> wrote:
    > Hi all,
    >
    > I appologise if this question is offtopic, hopefully it isn't though...
    >
    > I have:
    > Forum has_many :comments
    >
    > so obviously, I access the comments collection as
    > some_forum.comments
    >
    > check its type with:
    > some_forum.comments.class # => Array
    >
    > ok, that I expected, so I want to use Array#find on it, right?
    > some_forum.comments.find {...}
    >
    > but oops, I get:
    > ActiveRecord::RecordNotFound: Couldn't find Comment without an ID
    >
    > Hmm, that find is not the find I expected to find ;-) Looks like
    > an AR#find to me... but on an Array? It's not like I need SQL to
    > find certain elements of an Array... so I find this behaviour very
    > interesting. Am I doing something wrong here ? I can find the
    > element(s) I want from it, by iterating over it and selecting them,
    > but not by using find.
    >
    > BTW, it's Rails 1.1.6/AR 1.14.4 I'm having this behaviour on, haven't
    > tested it on other versions.
    >
    >
    > Thank you in advance,
    > Alex


    It would probably be ore appropriate to ask this on the rails-users
    mailing list, rather than this one.

    However, here's why you're seeing this behavior. An AR association,
    when accessed, returns a proxy object that looks like an Array, but
    has some extra methods: 'find', 'build', etc. For example:

    comments = some_forum.comments
    comments.class # => Array
    comments.build :foo => 'bar' # :build is obviously not a method on Array

    If you want to use Array#find or Array#find_all on an AssociationProxy
    object, you can call #detect and #select instead.

    Dave


    --
    Dave Goodlad
    or
    http://david.goodlad.ca/
     
    David Goodlad, Jan 25, 2007
    #2
    1. Advertising

  3. >>> sender: "David Goodlad" date: "Fri, Jan 26, 2007 at 08:16:30AM +0900" <<<EOQ
    Hi,

    > It would probably be ore appropriate to ask this on the rails-users
    > mailing list, rather than this one.

    Ok then, sorry for the noise, and a warm thank you for the
    explanation :)

    Have a nice day everyone,
    Alex
     
    Alexandru E. Ungur, Jan 26, 2007
    #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. Guest
    Replies:
    5
    Views:
    634
  2. Ville Vainio
    Replies:
    11
    Views:
    629
    Hamish Lawson
    Aug 10, 2004
  3. Stephan Diehl

    curious about slice behaviour

    Stephan Diehl, Sep 5, 2005, in forum: Python
    Replies:
    2
    Views:
    276
    Stephan Diehl
    Sep 5, 2005
  4. LAvoisieR
    Replies:
    8
    Views:
    440
    LAvoisieR
    Oct 27, 2005
  5. zefciu
    Replies:
    0
    Views:
    281
    zefciu
    Mar 10, 2007
Loading...

Share This Page