Flat resource into nested resource? #211
state-developer
started this conversation in
General
Replies: 2 comments 1 reply
-
Think I found a solution:
Hope that helps anyone else trying to do the same thing. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi @state-developer I think you can use compose_of method to aggregate member data, that makes to implement API with Alba much easier. If we don't want to use class MemberAddressResource
include Alba::Resource
attribute :lines, &:member_address1
attribute :city, &:member_city
attribute :state, &:member_state
end
class MemberInnerResource
include Alba::Resource
attribute :name, &:member_name
attribute :address do |inner|
MemberAddressResource.new(inner).to_h
end
end
class MemberResource
include Alba::Resource
attribute :number, &:membership_number
attribute :member do |foo|
MemberInnerResource.new(foo).to_h
end
end Alba currently doesn't have an API to define nested hash easily. If we need it so much I think we should implement it. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a Rails active record that looks like this:
I'm trying to figure out how to transform the object into something a 3rd party API needs (like this):
Changing the
membership_number
tonumber
is easy enough. However, I'm getting stuck trying to create the nested part of the JSON from the active record object. I've triedone
and various other ways of doing, like creating aMemberNameResource
andMemberAddressResource
, but nothing I've tried seems to work.Here is an example of what I've tried, but it didn't seem to work (simplified):
Any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions