【发布时间】:2009-05-16 17:16:34
【问题描述】:
我正在尝试为内部应用程序中的三个对象创建 ActiveResource 对象。
有Tags、Taggings、Taggables:
http://tagservice/tags/:tag
http://tagservice/taggings/:id
http://tagservice/taggables/:type/:key
Tag 的:tag 是 URL 编码的文字标记文本。 Tagging 的 :id 是一个自动递增的整数。 Taggable 的 :type 是一个字符串。没有有限的可标记类型集——该服务可以支持标记任何东西。 Taggable 的:key 是该Taggable 类型的服务分配的ID 字段。它可以是业务价值,例如员工的用户名,或者只是一个自动递增的整数。
如果这些是 ActiveRecord 对象,我会将它们编码为:
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
有谁知道ActiveResource 中的这些课程喜欢什么?谢谢!
【问题讨论】:
-
我的回答有帮助吗?如果是这样,你能接受吗? :) 如果没有,请告诉我你最终做了什么。
标签: ruby-on-rails ruby activerecord rest activeresource