I'm trying to create ActiveResource
objects for three objects in an internal application.
There are Tag
s, Tagging
s, and Taggable
s:
http://tagservice/tags/:tag
http://tagservice/taggings/:id
http://tagservice/taggables/:type/:key
Tag
's :tag
is the URL-encoded literal tag text. Tagging
's :id
is an autoincremented integer. Taggable
's :type
is a string. There is no finite set of taggable types -- the service can support tagging anything. Taggable
's :key
is the ID field that the service for that Taggable
's type assigns. It could be a business value, like an emplyee's username, or simply an autoincremented integer.
If these were ActiveRecord
objects, I'd code them something like this:
class Tag < ActiveRecord::Base
has_many :taggings
has_many :taggables, :through => :taggings
def self.find_by_id(id)
find_by_name(id)
end
def to_param
CGI::escape(self.name)
end
end
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable
end
class Taggable < ActiveRecord::Base
has_many :taggings
has_mnay :tags, :through => :taggings
def self.find_by_id(id)
find_by_type_and_key(*id.split('/'))
end
def to_param
"#{self.type}/#{self.key}"
end
end
Does anyone know what those classes would like like in ActiveResource
? Thanks!