Maddy.Shik
Maddy.Shik

Reputation: 6787

Map accessor method on activerecord to attribute on activeresource?

Some of attributes specified in ActiveModel are non db attributes which are just defined as getter setter. Problem is that these attributes values are not reflected on activeresource record on client side.

    #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 am getting nil value for attribute name for SepcialItem record on client. Basically i am trying to map accessor method name to name attribute on client side.

What is possible solution?

Upvotes: 3

Views: 744

Answers (1)

Dan
Dan

Reputation: 1745

ActiveResource is a means of communicating with a RESTful service and requires the class variable site to be defined, i.e.

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

This would utilize the default Rails collection and element conventions. So for a call to SpecialItem.find(1), ActiveResource would route to GET http://example.com/specialitems/1.json

Upvotes: 1

Related Questions