Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add extra virtual attribute to wrap_parameters

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...]
like image 623
user1883793 Avatar asked Oct 10 '13 04:10

user1883793


Video Answer


1 Answers

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]
like image 166
Jaco Pretorius Avatar answered Nov 02 '22 06:11

Jaco Pretorius