3

ActiveModel 中指定的一些属性是非 db 属性,它们只是定义为 getter setter。问题是这些属性值没有反映在客户端的活动资源记录上。

    #server side code
    class Item < ActiveRecord::Base
       #not null name attribute defined on db 
    end

   class SpecialItem < ActiveRecord::Base
      #nullable item_name attribute defined on db

      #association many to one for item defined here

      #name accessor 
      def name
         if !item_name.nil?
           return item_name
         else
           return item.name
         end 
      end  
   end

   #client side code
   class SpecialItem < ActiveResource::Base
        schema do
      attribute 'name', 'string'
        end
   end

我在客户端上的 SepcialItem 记录的属性名称的值为零。基本上我正在尝试将访问器方法名称映射到客户端的名称属性。

什么是可能的解决方案?

4

1 回答 1

1

ActiveResource 是一种与 RESTful 服务通信的方式,需要site定义类变量,即

   class SpecialItem < ActiveResource::Base
     self.site = 'http://example.com/'
     self.schema = { 'name' => :string}
   end

这将利用默认的 Rails 集合和元素约定。因此,对于 SpecialItem.find(1) 的调用,ActiveResource 将路由到GET http://example.com/specialitems/1.json

于 2012-04-07T03:36:53.757 回答