Ruby 是否将activerecord上的访问器方法映射到activeresource上的属性?
ActiveModel中指定的一些属性是非db属性,它们只是定义为getter setter。问题是这些属性值并没有反映在客户端的activeresource记录上Ruby 是否将activerecord上的访问器方法映射到activeresource上的属性?,ruby,ruby-on-rails-3,activerecord,model,activeresource,Ruby,Ruby On Rails 3,Activerecord,Model,Activeresource,ActiveModel中指定的一些属性是非db属性,它们只是定义为getter setter。问题是这些属性值并没有反映在客户端的activeresource记录上 #server side code class Item < ActiveRecord::Base #not null name attribute defined on db end class SpecialItem < ActiveRecord::Base
#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值。基本上,我正在尝试将访问器方法名称映射到客户端的name属性
#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
什么是可能的解决方案?ActiveResource是一种与RESTful服务通信的方法,需要定义类变量
site
,即
class SpecialItem < ActiveResource::Base
self.site = 'http://example.com/'
self.schema = { 'name' => :string}
end
class SpecialItem:string}
结束
这将利用默认的Rails集合和元素约定。因此,对于SpecialItem.find(1)的调用,ActiveResource将路由到GEThttp://example.com/specialitems/1.json