I have a model with some virtual attributes, and they are included in my form and gets submitted to the controller, but I cannot get the virtual attributes (SKU) to wrap inside the model hash
{"name"=>"Productname", "description"=>"Description", "sku"=>"ak0001", "product"=>{"name"=>"Productname", "description"=>"Description"}}
I can use the wrap_parameters to overwrite it, but I would have to add all attributes (virtual and non-virtual attributes) to the array, can't I just add the virtual attributes to the existing wrap parameter?
wrap_parameters Product, :include => [:sku, :name, ..etc...]
I had the exact same problem, I haven't found a great solution, but I have found one that seems slightly better. By default rails will try to determine the related model and call wrap_parameters with that model, so in your case
wrap_parameters Product
Which is exactly the same as
wrap_parameters Product, include: Product.attribute_names
So if you want to add a virtual attribute you can just do
wrap_parameters Product, include: Product.attribute_names + [:sku]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With