This project is archived and is in readonly mode.

#2425 ✓wontfix
avijayr (at gmail)

[PATCH] Add methods on active record to denote whether any associated objects exist for that association

Reported by avijayr (at gmail) | April 5th, 2009 @ 08:48 PM

hi, In many of my projects that deal with serializing an active record (usually to xml), we have to evaluate whether associated objects exist or not. If they do exist, then a separate call is made to fetch the associated objects (usually in a paged scenario). Rather than define the Proc inside the to_xml method, it would be better if the method is defined automatically in the AssociationReflection class. This patch will define a method with the question mark, and another to be used for the xml serialization.

Comments and changes to this ticket

  • CancelProfileIsBroken
  • pjammer

    pjammer August 8th, 2009 @ 06:40 PM

    +1 patch verified and all tests pass.

    It appears that this method would be very handy, when needed.

  • Nick Quaranto

    Nick Quaranto August 8th, 2009 @ 06:58 PM

    -1, I don't like the alias_method to has_#{name} and it seems that keeping a similar api to the dirty methods would be better. For example, if a Post has many Comments:

    post =

    instead of

    post =

    would be a lot cleaner.

  • Josh Nichols

    Josh Nichols August 8th, 2009 @ 07:00 PM

    +1 on the feature, -1 on the implementation.

    I agree with Nick on excluding has for the methods, ie comments? instead of has_comments?. This would be more in line with what you get from normal attributes.

  • Josh Susser

    Josh Susser August 8th, 2009 @ 07:26 PM

    I think post.comments.any? is a more consistent API. IMO it's better to keep the number of generated methods on the base class to a minimum, and layer this kind of behavior onto the association proxy object.

  • Dan Pickett

    Dan Pickett August 8th, 2009 @ 07:27 PM

    -1 on the implementation

    I agree with Nick on this as well.

    note that #49 was similar and denied - although this has more to do with verifying that the collection is empty or object exists

  • Jeremy Kemper

    Jeremy Kemper August 8th, 2009 @ 10:36 PM

    • State changed from “new” to “wontfix”

    Enumerable's already got the goods here, so let's not introduce a new overlapping API that only works with AR assocations.

  • Elad Meidar

    Elad Meidar August 8th, 2009 @ 10:45 PM

    Here's a patch for 2-3-stable removing the has_#{name} methods and keeps only the #{name}? methods.

  • Elad Meidar

    Elad Meidar August 8th, 2009 @ 10:46 PM

    forgot to mention that the patch is applied on master too.

  • Rizwan Reza

    Rizwan Reza August 8th, 2009 @ 10:51 PM


    +1 Patch works perfectly in master.

  • CancelProfileIsBroken

    CancelProfileIsBroken August 8th, 2009 @ 11:08 PM

    • Tag cleared.
    • Milestone cleared.
  • avijayr (at gmail)

    avijayr (at gmail) August 9th, 2009 @ 01:36 AM

    I would like to point out two things:
    1) The usage scenario described was for serialization to xml so that a corresponding ActiveResource could then be queried in a simple(shallow?) way before making a nested call. That is the reason why the implementation has the 'has_'. In the context of a ARecord, the method with '?' makes sense (to be compatible with Rails' notion of asking a question), but when you want to serialize the same, in my case in the to_xml method on ARecord, the '?' has to be treated specially - which is why I provided an alias without the '?'.

    As far as the presence of the 'has_' - this is present to differentiate between the query to retrieve the associated objects vs the presence of these associated objects.

    I hope I made myself clear as to why the implementation was the way it was.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>