I'm developing a REST Api for an app's factory generators to allow model instances to be also be created using REST, and I'm wondering how can I permit the nested attributes of the model without hardcoding the attributes.
Suppose I have a model called Restaurant
class Restaurant < ApplicationRecord
has_one :cost, dependent: :destroy, inverse_of: :grant
has_one :address, dependent: :destroy, inverse_of: :grant
has_one :owner, dependent: :destroy, inverse_of: :grant
accepts_nested_attributes_for :cost, :address, :owner
...
where its associations also have its own model attributes, and a factory
FactoryGirl.define do factory :restaurant, class Restaurant do
after(:create) do |restaurant|
restaurant.cost = assign_attributes(attributse_for(:cost_factory))
restuarnat.address = assign_attributes(attributes_for(:address_factory))
restaurant.owner = assign_attributes(attributes_for(:owner_factory))
end
...
end
where its nested associations also have its own factory. I am passing a json body through REST api in this format
{
"restaurant": {
"cost_attributes": {
"staff": 123
"tables": 123
}
}
I know I can permit the attributes these way
params.permit(:restaurant)
.permit(cost_attributes: [:staff, :tables],
address_attributes: [:address_attr_1],
owner_attributes: [:owner_attr_1]]
but the actual model I'm working on has many associations. It will be painful to hardcode everything. Is there a way i can permit the parameters coming through instead of hardcoding in my controller? Currently this is what im thinking
params.permit(Restaurant.nested_attributes_options.keys)
but obviously this does not work out.