This project is archived and is in readonly mode.

#1214 ✓stale
Franco Sellitto

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

Reported by Franco Sellitto | October 15th, 2008 @ 09:56 AM | in 3.x

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.

Comments and changes to this ticket

  • cbonnet99 (at gmail)

    cbonnet99 (at gmail) November 24th, 2008 @ 10:18 PM

    +1

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

  • Sigurd

    Sigurd November 25th, 2008 @ 12:51 PM

    • Assigned user set to “DHH”
  • Sigurd

    Sigurd November 25th, 2008 @ 12:51 PM

    • Assigned user cleared.
  • Pratik

    Pratik March 7th, 2009 @ 03:47 PM

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

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:04 PM

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

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:04 PM

    • State changed from “open” to “stale”

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages