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.

Serialization: (1) :methods with arguments and (2) scoped :include

#1214

Hello everybody

The to_xml and to_json serializers are pretty good for almost every purpose. However some extra feature would be nice to have in order to export a little more accurate data.

I tried to get a couple of improvements, keeping the old behaviors still working:

(1) With the :methods key we are able to include the result of instance methods assuming that are not accepting arguments or there are arguments with default value. The first improvement lets us to invoke a method with arguments, wrapping the method name and arguments into an array.


:methods => [:name]
:methods => [[:name, argument_1, argument_2, ...]]

(2) With the :include key we are able to include associated objects, however all instances are always associated. The second improvement lets us to scope the associated objects to include into the serialization.


:include => { :posts => { :records => @only_these_posts } }

I didn't try if a better approach would be scoping a subset of objects through named_scope, but in case of dynamic scope we should pass a lambda, so we still have to patch the serializer... isn't it?

To achieve these two behaviors, I changed just a couple of lines in rails/activerecord/lib/active_record/serialization.rb

Does it make sense? Or should I have a complete different approach?

Thanks.

Reported by Franco Sellitto · October 15th, 2008 @ 09:56 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. cbonnet99 (at gmail)
    cbonnet99 (at gmail)

    +1

    This behaviour would be really helpful, I think. Any news on including in Rails?

    November 24th, 2008 @ 10:18 PM

  2. Sigurd
    Sigurd
    • Assigned user set to DHH

    November 25th, 2008 @ 12:51 PM

  3. Sigurd
    Sigurd
    • Assigned user cleared.

    November 25th, 2008 @ 12:51 PM

  4. Pratik
    Pratik
    • Tag changed from json, serialization, serializer, to_json, to_xml, xml to json, patch, serialization, serializer, to_json, to_xml, xml
    • State changed from new to incomplete

    Patch doesn't have any docs/tests. It might be a better idea to discuss the issue on the core mailing list - http://groups.google.com/group/r...

    Thanks.

    March 7th, 2009 @ 03:47 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:04 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:04 PM