How to rename property/attribute of resource with jsonapi-resources ruby gem -
i building json api rails using jsonapi-resources gem. library great, lot of job.
however column names in our db not meaninful showed in api.
so, question: possible rename property/attribute in resource?
example:
let's have model user attribute login
.
class user < activerecord::base attr_accessor :login end
and want login
in api appear username
, e.g.:
class userresource < jsonapi::resource attribute :username, map_to: :login end
thanks!
set :username
alias :login
attribute:
class user < activerecord::base attr_accessor :login alias_attribute :username, :login end
then in jsonapi::resources
(jr) can specify username
attribute so:
class userresource < jsonapi::resource attribute :username end
by setting alias, you've mapped username
attribute login
attribute, doesn't matter whether use username
or login
, return same value.
Comments
Post a Comment