原来 ActiveResource 模型之间的连接是共享的。因此,如果您在一个模型中设置格式,它确实与其他模型中的格式不同。但是,如果您.connection.format
在两个单独的模型上调用该方法,则每次设置新格式时该格式都会更改。因此,如果Profile
第二次以格式加载,:json
那么.connection.format
两个模型都会变成ActiveResource::Formats::JsonFormat
我的原始问题完全不同(我不完全理解发生了什么)-您可以查看编辑历史以查看原始版本。希望我能得到更多的回应...
证明:
class Location < ActiveResource::Base
self.format = :xml
end
class Profile < ActiveResource::Base
self.format = :json
end
然后在一rails console
...
>> Location.format
=> ActiveResource::Formats::XmlFormat
>> Location.connection.format
=> ActiveResource::Formats::XmlFormat
到目前为止一切顺利......Location
模型及其连接具有正确的格式。
>> Profile.format
=> ActiveResource::Formats::JsonFormat
看起来很正常,这Profile
就是我想要的格式。
>> Location.format
=> ActiveResource::Formats::XmlFormat
好的... Location.format 在加载 Profile 模型后仍然相同 注意:这些模型是延迟加载的,因此在您尝试调用类名之前不会包含它们的文件和代码。
>> Location.connection.format
=> ActiveResource::Formats::JsonFormat
问题就从这里开始了。在我们调用 Profile 模型之后,它完成了Location.connection.format
>> Profile.connection.format
=> ActiveResource::Formats::JsonFormat
格式不应该相同。当您调用 Location.find(:all, :from => "/something.xml") 之类的内容时,这会导致解析完全中断 - 它会尝试解析xml
asjson
我想我现在的问题是 - 我如何分离这两个连接?(或以其他方式解决此问题)
编辑以在控制台中添加此测试:
>> Location.connection == Profile.connection
=> true