A problem with the RESTful ActiveResource class is that it doesn’t get any
kind of schema from the server. For GET/Retrieve of CRUD, it’s not a
problem. For POST/CREATE it is.
We put code like this in our ActiveResource method’s initialize:
class MyRestfulResourcet < ActiveResource::Base
self.site = "http://mysite/"
self.collection_name = "myrecords"
self.element_name = "myrecord"
Schema = [ :name, :type, :firstboot, :subscriber_id ]
def self.find_single(x, options)
return nil if x.nil?
return nil if x.to_i < 1
super(x, options)
end
def initialize(attributes = {})
super(attributes)
if @attributes["type"].nil?
@attributes["type"] = "TheSubclass"
end
Schema.each { |attr|
attr = attr.to_s
if @attributes[attr].nil?
@attributes[attr] = nil
end
}
end
end
This initializes the @attributes hash to contain the things that we need, and
so appropriate attribute accessor methods will be created.
Maybe there is a better way.