Usually if I want to map params in this way (usually due to some external API) I use the alias_attribute
method in ActiveRecord
So if I have params that come to me as {name: "bob", alias_id: 1234}
and ideally I would want to have {name: "bob", foreign_id: 1234}
I declare the following in my ActiveRecord
model
class MyModel < ActiveRecord::Base
alias_attribute :alias_id, :foreign_id
end
Now my model will always recognise alias_id
as mapping to foreign_id
in any context and I can push in params.require(:name, :alias_id)
and my model will recognise it and process it as required without looping over attributes in my controller.
This is also simpler if you want to do it on other models as well.