her
her copied to clipboard
Don't pass the parent ID in a nested resource create
Is there any way to influence Her to not pass the parent ID, when performing a create of the nested resource.
Given the code:
it 'creates a member successfully' do
@group = Group.find('tstream:Canary-UI_Discuss')
member = @group.members.create({"id"=>"tt3683", "kind"=>"person"})
end
Her generates
- request:
method: put
uri: http://IDP:[email protected]:8082/activitystream/v4/groups/tstream:Canary-UI_Discuss/members/tt3683
body:
encoding: UTF-8
string: '{"member":{"id":"tt3683","kind":"person","group_id":"tstream:Canary-UI_Discuss"}}'
headers:
User-Agent:
- Faraday v0.8.8
Content-Type:
- application/json
Accept:
- '*/*'
I would like to eliminate the group_id
from the body as the server rejects it as an invalid field.
I've tried setting the value to nil in a before_create, but this did not influence the result.
@billc hey Bill - Appreciate this is a really old issue, but did you ever find a solution? I'm banging into the same problem today. In my case the ID is passed in the URL and doesn't need including in the body, doing so throws an error much like your own.
@SirRawlins I did not find an elegant solution and have since moved on to a different project. My hack is below. Its at least functional. Hope it helps.
it 'creates a member successfully' do
@group = 'tstream:Canary-UI_Discuss'
member = Member.add(@group, {"id"=>"tt3683", "kind"=>"person"})
end
class Member
include Her::Model
collection_path "groups/:group_id/members"
primary_key :clientId
include_root_in_json true
#belongs_to :group
attributes :id, :clientId, :kind, :name, :url
def self.add(group_id, attributes)
# must include root in json manually
payload = Hash.new
payload["member"] = attributes
post_raw "groups/#{group_id}/members", payload
end
def self.destroy_existing(group_id, id)
delete_raw "groups/#{group_id}/members/#{id}"
end
end