3

ActiveModel で指定された属性の一部は、getter setter として定義されている非 db 属性です。問題は、これらの属性値がクライアント側のアクティブなリソース レコードに反映されないことです。

    #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 レコードの属性名に nil 値を取得しています。基本的に、アクセサメソッド名をクライアント側の名前属性にマップしようとしています。

可能な解決策は何ですか?

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 に答える