#298 √ resolved
Matt Colyer

to_json broken for ActiveRecord

Reported by Matt Colyer | May 5th, 2008 @ 02:40 AM | in 0.9.4

In merb-core 0.9.2 the json gem overrides to_json so that the activerecord json support (which allows you to exclude attributes) is broken.

I am not really sure how to resolve this. I took the json gem out but I am not sure that this is the best solution going forward.

Comments and changes to this ticket

  • Michael Klishin (antares)

    Michael Klishin (antares) May 5th, 2008 @ 10:35 PM

    • → Milestone changed from “” to “0.9.4”
    • → State changed from “new” to “invalid”
    • → Assigned user changed from “” to “Michael Klishin (antares)”

    It did work for me in 0.9.2 and works now in 0.9.3. Check out display method documentation that shows how you can do :excludes and :only-s passed to to_json method of your ActiveRecords.

    Doc generator at merbivore.com only uses Notes section (weird) so he is a pastie for you.

  • Michael Klishin (antares)

    Michael Klishin (antares) May 5th, 2008 @ 10:36 PM

    By the way, I use frozen framework and bundle ActiveRecord with my app. Try to do it as well. It is a good practice anyway since it makes you env independent.

  • Matt Colyer

    Matt Colyer May 6th, 2008 @ 07:55 AM

    This ticket isn't invalid. Could you please re-open it?

    The first issue, thanks for pointing out the documentation. I would have never known to go look for it as when I did go looking for it appeared undocumented. That said I believe that the documentation being incorrectly generated (to the point where it doesn't include information) is a bug. Can this be resolved?

    The second issue, I tried the new syntax like this:

    "display @conversations, :include => {:only => [:title, :id], :messages => {:only => :from}}"

    and it yields the incorrect results (only the only option works).

    If I use this (with the json gem disabled):

    "render @conversations.to_json(:only => [:title, :id], :include => {:messages => {:only => :from}})"

    it renders the correct results.

    Is there something else that I need to do? I feel like there is some difference between our setups.

  • Michael Klishin (antares)

    Michael Klishin (antares) May 7th, 2008 @ 11:51 AM

    • → State changed from “invalid” to “open”

    I doubt you can specify only option inside include option. But anyway, what can be done for this to be fixed in merb-core, if it is json gem specific issue?

  • Matt Colyer

    Matt Colyer May 8th, 2008 @ 07:14 PM

    Here is a link to the rails documentation which explains the syntax:

    http://www.railsbrain.com/api/ra...

    I am thinking that adding a section to the documentation describing the situation would be one fix. The other fix would be to override that method in merb-core so that the options get passed through correctly to the active record objects.

  • Michael Klishin (antares)

    Michael Klishin (antares) May 8th, 2008 @ 09:17 PM

    There is an example for activerecord in documentation for display method. It just gotten ate by Merb documentation generator :)

    You can see it in source. If you think anything else should be added, just do it and then make a patch with git-format-patch.

    Thank you.

  • Matt Colyer

    Matt Colyer May 12th, 2008 @ 08:19 AM

    It turns out that upgrading to 0.9.3 almost fixed the problem. If I disable the json gem it works correctly. However if I don't disable the gem I get this error:

    wrong argument type Hash (expected Data)

    /var/lib/gems/1.8/gems/merb-core-0.9.3/lib/merb-core/controller/mixins/render.rb:220:in `to_json'

    /var/lib/gems/1.8/gems/merb-core-0.9.3/lib/merb-core/controller/mixins/render.rb:220:in `send'

    /var/lib/gems/1.8/gems/merb-core-0.9.3/lib/merb-core/controller/mixins/render.rb:220:in `display'

    for this line:

    display @conversations, {:only => [:title, :id], :include => {:messages => {:only => :from}}}

    Ideas?

  • Fabien Franzen (loob2)

    Fabien Franzen (loob2) May 15th, 2008 @ 01:11 AM

    • → State changed from “open” to “resolved”

    Matt,

    I'm not sure you're disabling the json gem like it's intended: http://pastie.caboo.se/197154 though it sounds like you do. In any case, I implemented this so that you can use ActiveSupport's to_json (which is used by ActiveRecord) in Merb without getting into issues like you describe. In other words, you need to choose which implementation you prefer/need and disable the json gem in your config if you want compatibility with AR.

    I'm closing this ticket for now, feel free to chime in again.

  • Matt Colyer

    Matt Colyer May 19th, 2008 @ 06:40 AM

    Is there any difference with using this instead?

    Merb.disable :json

  • Fabien Franzen (loob2)

    Fabien Franzen (loob2) May 19th, 2008 @ 09:11 AM

    Merb.disable :json is exactly the same. You can do this at the top of your init.rb

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

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

Tags