How to access a map on activerecord for an attribute on activeresource?

Some attributes specified in ActiveModel are not db attributes, which are simply defined as getter setter. The problem is that these attribute values ​​do not affect client-side activeresource records.

    #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

I get the nil value for the attribute name for the SepcialItem entry on the client. Basically I am trying to match the name of the access method to name the attribute name on the client side.

What is a possible solution?

+3
source share
1 answer

ActiveResource is a means of communicating with a RESTful service and requires that a class variable sitebe defined, i.e.

   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

+1

All Articles