SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Rails] Finding only the COUNT of a has_many association

    I'm currently writing some software in which I just want to know the amount of has_many items a record has. I am under the impression that doing:
    Code:
    something.hasmanys.length
    Will pull all the "hasmanys" from the database, so it would be a bog on the server if I were to do this? Or if I called this would it only call a COUNT() query? (which I am trying to do)
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check the logs ;-).

    I don't know about #length, but #size (and #count too) use a COUNT sql select.

    From the docs:

    Firm#clients.size (similar to Client.count "firm_id = #{id}")
    So:

    Code:
    something.hasmanys.size

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply, I didn't get a chance to really check this until today, but you were right! Thanks!

    #count and #size indeed only use a count(*) query Thanks
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •