I’ve ended up with a nested accepts_nested_attributes_for with three models, which is probably not a good idea but not much can be done about it right now:
fairsharing_record → organisation_link → organisation
The controller creates records as follows:
def create
@fairsharing_record = FairsharingRecord.new(current_user: current_user)
puts permitted_attributes(@fairsharing_record) # The output of this is below.
@fairsharing_record.update(permitted_attributes(@fairsharing_record).except(:logo)) # fails here
# Various other things happen before the record is saved...
My permitted attributes contain this:
...
{organisation_links_attributes: [:relation, :is_lead, :organisation_attributes =>
[:name, :homepage, :organisation_type_ids => [], :alternative_names => [],
:country_ids => []]]},
...
…and there’s a test to check that an organisation and its link are created:
test 'can create organisations via fairsharing_record' do
params = {
fairsharing_record: {
record_type_id: record_types(:repository).id,
organisation_links_attributes: {
relation: 'maintains',
is_lead: true,
organisation_attributes: {
organisation_type_ids: [organisation_types(:university).id],
name: 'Example University',
homepage: 'https://example.edu',
alternative_names: ['alternate example university'],
country_ids: [countries(:duncruigh).id],
}
}
}
}
post '/fairsharing_records', params: params.to_json, headers: auth_headers(users(:one)) # fails here
assert_response :success
body = JSON.parse(response.body)['data']
assert_not_empty body
record = FairsharingRecord.find(body['id'].to_i)
assert_not_empty record.organisation_links
puts record.organisation_links.inspect
end
As far as I can tell all the correct parameters appear; the puts statement above shows this:
{"record_type_id"=>486430670, "organisation_links_attributes"=>#<ActionController::Parameters {"relation"=>"maintains", "is_lead"=>true, "organisation_attributes"=>#<ActionController::Parameters {"name"=>"Random University", "homepage"=>"https://rando.edu", "organisation_type_ids"=>[544900590], "alternative_names"=>["stochastic university"], "country_ids"=>[671028764]} permitted: true>} permitted: true>, "metadata"=>#<ActionController::Parameters {"name"=>"Nested Organisation Record", "abbreviation"=>"NOR", "status"=>"ready", "homepage"=>"https://example.edu", "doi"=>"XXYY1122", "tombstone"=>false, "contacts"=>[#<ActionController::Parameters {"contact_name"=>"John Smith", "contact_orcid"=>"0000-1111-2222-333X", "contact_email"=>"jsmith@example.com"} permitted: true>], "description"=>"Testing nested organisations."} permitted: true>}
As far as I can tell that looks correct, yet a TypeError: no implicit conversion of Symbol into Integer
is a result. It’s clearly related to the organisation_links_attributes (the error disappears if I remove the non-array parameters from the allowed_fields) but I can’t work out what. Could anyone suggest how this might be fixed?