Issue with collection when collection consists of one member

5 views
Skip to first unread message

Jeroen van Dijk

unread,
Dec 27, 2010, 10:08:27 AM12/27/10
to rest...@googlegroups.com
Hi all,

I have the following code:

collection(@activities) do |collection|
  collection.members do |member, activity|
    member.values { |values|
      values.id           activity.id
      values.title        activity.title
      # mores stuff ...
    }
  end
end

If activities is an array with two or more items everything goes fine (the generated json contains an array), however if the list is only one item the generated json does not contain an array but just one item. So the following is expected:

      """
      { "activities": 
        [ 
          {
            "id": 1,
            "title": "likes your app x",
          }
        ]
      }
      """   

But I get: 

      """
      { "activities": 
          {
            "id": 1,
            "title": "likes your app x",
          }
      }
      """  

I find it hard to debug this and I also don't see tests for this behaviour so I find it hard to where to start. Any ideas?

Regards,
Jeroen

Guilherme Silveira

unread,
Dec 27, 2010, 10:11:37 AM12/27/10
to rest...@googlegroups.com

Hello jeroen. You can open json_spec test and add the test case for me? I can implement the fix for your version and trunk.which version are you using?

Regards

Jeroen van Dijk

unread,
Dec 27, 2010, 10:29:25 AM12/27/10
to rest...@googlegroups.com
Hi Guilherme,

I'm using restfulie (0.9.3), instead of just the tokamak gem because of the template handlers for Rails. I just tried to write a failing spec in tokamak test suite, but everything seems fine there. Are you gonna update the Restfulie gem? It might be fixed possibly. 

Regards,
Jeroen

PS I think it would be good if you add .gemspec files to repositories so it easier to try out the github versions of the gems.

Jeroen van Dijk

unread,
Dec 27, 2010, 11:36:50 AM12/27/10
to rest...@googlegroups.com
I'm currently trying to get the tokamak gem without the restfulie gem to work with Rails, but without luck. I think the Restfulie gems is providing the view helpers like #collection and #member. Is there an example to use the latest tomaka gem with Rails without Restfulie?

Cheers,
Jeroen

Guilherme Silveira

unread,
Dec 27, 2010, 11:42:59 AM12/27/10
to restfulie
Hi Jeroen,

You are right, the gem right now is built to be used with the trunk
version of restfulie (soon to become beta 2). Do you need to use it
standalone? Let me know what is the code you need to work and I get
the beta 2 out tonight with support to it (both on the tokamak and
restfulie gems).

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Mon, Dec 27, 2010 at 2:36 PM, Jeroen van Dijk

Jeroen van Dijk

unread,
Dec 27, 2010, 12:42:45 PM12/27/10
to rest...@googlegroups.com
Hi Guilherme,

Thanks for responding so quickly. I don't need to use Tokamak standalone perse, it is just that I thought the tokamak standalone was a newer version of tokamak, but I think I was confused because I didn't look at the Restfulie code on github, just in the gem itself.

I've put part of the cucumber scenario into a gist: https://gist.github.com/756334 . Please let me know if you need to know more. 

Regards,
Jeroen

Guilherme Silveira

unread,
Dec 27, 2010, 1:41:04 PM12/27/10
to restfulie
Hi Jeroen,

I will take a look at it today and this evening, will get back to you!
I will create the test case and fix on the old version to ensure
backward compatibility.

Do you need to use 0.9.3 or are you able to start using 1.0.0.beta 2?

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Mon, Dec 27, 2010 at 3:42 PM, Jeroen van Dijk

Jeroen van Dijk

unread,
Dec 28, 2010, 2:51:43 AM12/28/10
to rest...@googlegroups.com
Hi Guilherme,

Thanks for looking it to it. I'm able to use any version as long as it works with Rails 3.0.3. 

Jeroen

Guilherme Silveira

unread,
Jan 6, 2011, 1:33:14 PM1/6/11
to rest...@googlegroups.com
Hi Jeroen,

Tokamak 1.1.1 is out (thanks to Cirpiani) and Restfulie 1.0.0.beta4
too. To install it you need to gem install restfulie --pre (do not
forget the pre).
Docs are being updated, and except for possible bugs, as soon as the
docs are finished we have 1.0.0.

Can you check if it works with 1.0.0.beta4?

Let me know if you need something./

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Tue, Dec 28, 2010 at 5:51 AM, Jeroen van Dijk

Jeroen van Dijk

unread,
Jan 25, 2011, 5:48:44 AM1/25/11
to rest...@googlegroups.com
Hi Guilherme,

Thanks for the update. Sorry for getting back this late. I finally had time to update to the new gem yesterday. I got everything working now, but I had to go through some hoops to get it working:

I had to change the Gemfile to include the following instead of just having 'gem "restfulie":

gem 'restfulie', '1.0.0.beta4'
gem 'tokamak', '1.1.2'
gem 'medie'
gem 'respondie'

Otherwise ruby would complain that it could not find tokamak, medie and respondie. So I'm guessing something is not setting the load paths correctly or something is wrong with the dependencies, I have not investigated that yet.

Another thing that I noticed was that I had to add the :root option to some of the member declarations that I didn't have to before:

E.g. previously I had:
collection(@schemes) do |collection|
  collection.members do |member, scheme|

Now I had to do:

collection(@schemes) do |collection|
  collection.members(:root => "schemes") do |member, scheme|

So I guess the default changed to not have a root?

Regards,

Jeroen

Guilherme Silveira

unread,
Jan 25, 2011, 11:26:28 AM1/25/11
to restfulie
Hi Jeroen!

Great... Donizetti has just fixed the gem requirements yesterday (will
be out on 1.0.0), if you want you can try with out with the trunk
version.

> So I guess the default changed to not have a root?

You are right. This was a tokamak decision when it was extracted from
Restfulie. There is a branch where we tried to implement support to
default root naming, but we came across a few issues. The most
important one was: if you are rendering an array and it has one
element, it is easy to guess the name, but if it has no elements, it
is impossible to guess the type name because there is no type
definition into it.
Any thoughts? We can provide a setting that would provide a default
root element, but it would not work properly with empty arrays. I can
do it for you, do you need it?


> Regards,


Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Tue, Jan 25, 2011 at 8:48 AM, Jeroen van Dijk

Jeroen van Dijk

unread,
Jan 25, 2011, 11:54:04 AM1/25/11
to rest...@googlegroups.com
Hi Guilherme,

On Tue, Jan 25, 2011 at 5:26 PM, Guilherme Silveira <guilherme...@caelum.com.br> wrote:
Hi Jeroen!

Great... Donizetti has just fixed the gem requirements yesterday (will
be out on 1.0.0), if you want you can try with out with the trunk
version.

Cool I'll try it soon. 
 
> So I guess the default changed to not have a root?
You are right. This was a tokamak decision when it was extracted from
Restfulie. There is a branch where we tried to implement support to
default root naming, but we came across a few issues. The most
important one was: if you are rendering an array and it has one
element, it is easy to guess the name, but if it has no elements, it
is impossible to guess the type name because there is no type
definition into it.

True. I completely understand that. But I saw that my collections in some cases were named "members" instead of the resource name when I leave out the :root option. Is this intended behaviour then? If so, I don't completely understand how that would give extra information.
 
Any thoughts? We can provide a setting that would provide a default
root element, but it would not work properly with empty arrays. I can
do it for you, do you need it?

No I think I don't need that setting. I was just wondering. I currently rely on having a root element of "activities" for the activities resources etc. In the future I want to use Restfulie clients to interact with my own API. So to me the most important is that I will have a Restfulie compliant API without modifications.

Best,
Jeroen

Guilherme Silveira

unread,
Jan 26, 2011, 9:59:38 AM1/26/11
to restfulie
Hi Jeroen,

So let's stick with the mandatory root element for this release!
Is there anything else you need?

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Tue, Jan 25, 2011 at 2:54 PM, Jeroen van Dijk

Jeroen van Dijk

unread,
Jan 27, 2011, 9:03:43 AM1/27/11
to rest...@googlegroups.com
Hi Guilherme, 

I'm pretty happy with what you guys have done! One thing that could be improved though is error messages, see below.

Thanks for the great product so far :)

Jeroen

In the case below I was forgotten to call respond_with in the controller. Giving me the error below:

      You have a nil object when you didn't expect it!
      You might have expected an instance of Array.
      The error occurred while evaluating nil.[] (ActionView::Template::Error)
      /Users/jvandijk/.rvm/gems/ruby-1.8.7-p330/gems/tokamak-1.1.2/lib/tokamak.rb:10:in `builder_lookup'
      ./app/views/api/v1/network_accounts/update.tokamak:1:in `_app_views_api_v__network_accounts_update_tokamak___1755326573_2188597200_0'

Jeroen van Dijk

unread,
Jan 27, 2011, 9:55:39 AM1/27/11
to rest...@googlegroups.com
Hi Guilherme,

In hindsight, I do think that it would be nice to have Tokamak be a bit more flexible in the structure of the data. I have an application where I have some legacy JSON views that are structured the rails way:

[{ "user : { "field1":"value1"}}]

Instead of the Restfulie way:

{"users: [{"field1":"value1"}]}

In this case, I would like to go the Restfulie way, but many clients rely on the old Rails way. So it would be a bad idea to change it over to Tokamak and make everyone incompatible. I think it would be a good feature if I could make the changes in small steps; first change over to use Tokamak without changes needed in the integration tests nor clients. Then when everyone is ready for it change to the new structure. Does that make sense?

Is this maybe where Recipes could come in handy? Maybe have a Rails recipe and a Restfulie Recipe?

Looking forward to your insights.

Jeroen

Guilherme Silveira

unread,
Jan 28, 2011, 7:06:54 AM1/28/11
to restfulie
Hi Jeroen,

It seems that it makes sense to be Rails compatible actually. Although
the second version would save a few bytes, the first one allows
multiple types within the array...

Nick Sutterer also pointed a warning on tokamak due to object_id and
that the code method should return an integer instead of a string.
I believe we need those two fixed and the best that we can do on the
array side before 1.0 (the it should be compatible up to at least 1.1)

Can you raise the issue at github? I will take care of it

Regards


Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Thu, Jan 27, 2011 at 12:55 PM, Jeroen van Dijk

Jeroen van Dijk

unread,
Jan 28, 2011, 7:47:25 AM1/28/11
to rest...@googlegroups.com
Hi Guilherme,


Can you raise the issue at github? I will take care of it


Sure, but I guess you mean creating an issue here http://http://restfulie.lighthouseapp.com ? Your github accounts do not seem to have activated issue tracking.


Regards

Jeroen

Guilherme Silveira

unread,
Jan 28, 2011, 8:22:55 AM1/28/11
to restfulie
> Sure, but I guess you mean creating an issue here
> http://http://restfulie.lighthouseapp.com ? Your github accounts do not seem
> to have activated issue tracking.
Yes... its just the habit, most of the other os projects we develop
have issues registered at github.

Thanks!

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Fri, Jan 28, 2011 at 10:47 AM, Jeroen van Dijk

Jeroen van Dijk

unread,
Jan 31, 2011, 4:02:41 AM1/31/11
to rest...@googlegroups.com

Guilherme Silveira

unread,
Feb 2, 2011, 7:11:57 AM2/2/11
to restfulie
Hi Jeroen,

Is the problem happening with an array internal to another object
being serialized or is it a root array?

Regards
Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

On Mon, Jan 31, 2011 at 7:02 AM, Jeroen van Dijk

Jeroen van Dijk

unread,
Feb 2, 2011, 7:19:51 AM2/2/11
to rest...@googlegroups.com
The problem is happening with the root array. So currently the root array always needs to be nested in a hash. It can't be just an array at root level like the default Rails way. Do you want me to give a concrete code example of what I mean?

Guilherme Silveira

unread,
Feb 2, 2011, 9:18:42 AM2/2/11
to rest...@googlegroups.com

Got it. If you can send a sample

2011. 2. 2. 오전 10:19에 "Jeroen van Dijk" <jeroentj...@gmail.com>님이 작성:



The problem is happening with the root array. So currently the root array always needs to be nested in a hash. It can't be just an array at root level like the default Rails way. Do you want me to give a concrete code example of what I mean?



On Wed, Feb 2, 2011 at 1:11 PM, Guilherme Silveira <guilherme...@caelum.com.br> wrote:
>

> H...

Jeroen van Dijk

unread,
Feb 2, 2011, 9:46:06 AM2/2/11
to rest...@googlegroups.com
I have created this gist https://gist.github.com/807773 . I hope this makes it more clear what I mean, if not please let me know.

Regards, 

Jeroen

2011/2/2 Guilherme Silveira <guilherme...@caelum.com.br>

Guilherme Silveira

unread,
Feb 3, 2011, 9:27:22 AM2/3/11
to restfulie
Hi Jeroen,

Great. What about:

array(@users) do |x|
x.members(:root => "users") do |member, user|
member.values { |values|
values.id user.id
values.name user.name
values.email user.email
}
end
end

What do you think? I think it should be called an array as both maps
and arrays are collections and its ambiguous in json.

Regards

Guilherme Silveira
Caelum | Ensino e Inovação
http://www.caelum.com.br/

2011/2/2 Jeroen van Dijk <jeroentj...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages