rails activeresource messages
-
08-07-2019 - |
Question
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?
Solution
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.
OTHER TIPS
Put the following in your intializers:
class ActiveResource::Connection
# Creates new Net::HTTP instance for communication with
# remote service and resources.
def http
http = Net::HTTP.new(@site.host, @site.port)
http.use_ssl = @site.is_a?(URI::HTTPS)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
http.read_timeout = @timeout if @timeout
#Here's the addition that allows you to see the output
http.set_debug_output $stderr
return http
end
end
Search '"http.set_debug_output $stderr" "ActiveResource"' to find the original author of this helpful tip.