0

I have product, feature, product_feature, and quota.

# product.rb

  has_many :product_features, dependent: :destroy
  has_many :quotas, through: :product_features
  has_many :features, through: :product_features

  accepts_nested_attributes_for :product_features
# product_feature.rb

  belongs_to :product
  belongs_to :feature
  has_one :quota, dependent: :destroy

  accepts_nested_attributes_for :feature
# quota.rb

  belongs_to :product_feature
# feature.rb

  has_many :product_features, dependent: :destroy
  has_many :quotas, through: :product_features
  has_many :products, through: :product_features

I'm trying to add quota into the nested attributes to allow a POST to product, to create product, features, product_features, and quotas all in one go.

Right now, my product_params method looks like:

      def product_params
        params.require(:product).permit(:name, :status, :description,
                                        product_features_attributes: [{ feature_attributes: %i[name company_id] }])

How do I add quotas into this complex nested attributes?

Quota belongs_to product_feature, so a product_feature must be created for a quota to be created. That leads me to believe quota should accept nested attributes for product_feature. However, not every product_feature will have a quota, so i'm not sure how that would work.

2
  • I suggest building the deeply nested form the way you want, then you can see how the params come through and mock your params.permit to suite. Commented Aug 17, 2022 at 2:48
  • Also, you don't have to use whitelisted params if it's too deeply nested. You can use Hash.dig to get values as you want them. Commented Aug 17, 2022 at 2:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.