views:

91

answers:

1

I have a quick question about active resource. If I have a User resource, when I do a call like

User.find(1).put(:promote, :position => 'manager')

According to the api it translates into this call /users/1/promote.xml?position=manager

My question is this. Is activeresource actually making two calls here? find doing a get, then putting on that object, or does appending .put to the .find mean that it just makes a single call. If this is so, then the only reason for the .find is the give the proper url format of /users/:id/promote ??

I couldn't find in the docs where this might be specified, but it's the .find that makes me think maybe two service calls are taking place?

A: 

If ActiveResource works like ActiveRecord, I would say 'yes'. If you do something like

Foo.find(1).update_attributes(:name=>"Bar")

ActiveRecord first does a select to get the object and then issues an update call to the database to change the record. I would presume that ActiveResources functions in a similar manner where it issues two web services calls to get the object and then update the object.

Scott
I'm hoping this isn't the case, that would seem incredibly poor from a service performance standpoint. Since it's all xml messages, the initial find does nothing for me, except that just calling User.put(:promote...) doesn't return a user object, but rather a hash of user attributes... so the api recommends using User.find(x).put... I'm still interested though so know if anyone has a definitive answer
brad
You could run a packet sniffer, e.g. WireShark, to test this definitively.
Schrockwell
The thing of it is, Rails needs to verify that User.find(1) actually exists and doesn't return nil, otherwise it's trying to perform a put operation on a non-existent record. This leads me to believe that it does two calls, but I agree with @Schrockwell, packet sniffer FTW
Scott