我有一个关于活动资源的快速问题。如果我有用户资源,当我打电话时

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

根据api,它转换为此调用/users/1/promote.xml?position=manager

我的问题是这个。 activeresource实际上是在这里进行两次调用吗?找到一个get,然后加上该对象,或者将.put附加到.find意味着它只是进行一次调用。如果是这样,那么.find的唯一原因就是给出了/ users /:id / promote的正确url格式?

我在文档中找不到这个可能被指定的内容,但是这个.find让我觉得可能正在进行两次服务调用?

有帮助吗?

解决方案

如果ActiveResource像ActiveRecord一样工作,我会说'是'。如果您执行类似

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

ActiveRecord首先执行select以获取对象,然后发出对数据库的更新调用以更改记录。我假设ActiveResources以类似的方式运行,它发出两个Web服务调用来获取对象,然后更新对象。

其他提示

将以下内容放入您的初始化者中:

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

搜索'" http.set_debug_output $ stderr" "&的ActiveResource QUOT;”找到这个有用提示的原作者。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top