Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#2425

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.

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

State: wontfix
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. CancelProfileIsBroken
  2. pjammer
    pjammer

    +1 patch verified and all tests pass.

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

    August 8th, 2009 @ 06:40 PM

  3. Nick Quaranto
    Nick Quaranto

    -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 = Post.new
    post.comments?
    

    instead of

    post = Post.new
    post.has_comments?
    

    would be a lot cleaner.

    August 8th, 2009 @ 06:58 PM

  4. Josh Nichols
    Josh Nichols

    +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.

    August 8th, 2009 @ 07:00 PM

  5. Josh Susser
    Josh Susser

    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.

    August 8th, 2009 @ 07:26 PM

  6. Dan Pickett
    Dan Pickett

    -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

    August 8th, 2009 @ 07:27 PM

  7. Jeremy Kemper
    Jeremy Kemper
    • 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.

    August 8th, 2009 @ 10:36 PM

  8. Elad Meidar
    Elad Meidar

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

    August 8th, 2009 @ 10:45 PM

  9. Elad Meidar
    Elad Meidar

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

    August 8th, 2009 @ 10:46 PM

  10. Rizwan Reza
    Rizwan Reza

    verified.

    +1 Patch works perfectly in master.

    August 8th, 2009 @ 10:51 PM

  11. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag cleared.
    • Milestone cleared.

    August 8th, 2009 @ 11:08 PM

  12. avijayr (at gmail)
    avijayr (at gmail)

    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.

    August 9th, 2009 @ 01:36 AM