arrange_as_array causing unknown error
mices opened this issue · 7 comments
I'm using item_type model name instead of category, i know that was a mistake but now arrange_as_array seems to be erroring out ...
Started POST "/abc/items" for 127.0.0.1 at 2023-03-01 04:55:25 -0500
04:55:25 web.1 | Processing by ItemsController#create as TURBO_STREAM
04:55:25 web.1 | Parameters: {"authenticity_token"=>"[FILTERED]", "item"=>{"images"=>[""], "item_type_id"=>"6", "title"=>"abc", "description"=>"abc", "item_item_properties_attributes"=>{"0"=>{"item_property_id"=>"68", "item_id"=>"", "text_value"=>""}, "1"=>{"item_property_id"=>"75", "item_id"=>"", "text_value"=>""}, "2"=>{"item_property_id"=>"76", "item_id"=>"", "text_value"=>"No"}, "3"=>{"item_property_id"=>"77", "item_id"=>"", "text_value"=>"Dreamworks"}, "4"=>{"item_property_id"=>"78", "item_id"=>"", "text_value"=>""}, "5"=>{"item_property_id"=>"79", "item_id"=>"", "text_value"=>"Good"}, "6"=>{"item_property_id"=>"81", "item_id"=>"", "text_value"=>""}, "7"=>{"item_property_id"=>"82", "item_id"=>"", "text_value"=>""}}, "box_id"=>"", "envelope_id"=>""}, "commit"=>"Create Item"}
04:55:25 web.1 | User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
04:55:25 web.1 | ItemType Load (1.7ms) SELECT `item_types`.* FROM `item_types` ORDER BY name
04:55:25 web.1 | ↳ app/models/item_type.rb:12:in `arrange_as_array'
04:55:25 web.1 | TRANSACTION (0.7ms) BEGIN
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (1.0ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 68 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (1.1ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 75 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (1.1ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 76 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (1.0ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 77 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (1.2ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 78 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (0.9ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 79 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (0.8ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 81 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | ItemProperty Load (0.8ms) SELECT `item_properties`.* FROM `item_properties` WHERE `item_properties`.`id` = 82 LIMIT 1
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | TRANSACTION (0.5ms) ROLLBACK
04:55:25 web.1 | ↳ app/controllers/items_controller.rb:69:in `block in create'
04:55:25 web.1 | Rendering layout layouts/application.html.erb
04:55:25 web.1 | Rendering items/new.html.erb within layouts/application
edit: kbrock added code block
It errors out on line 12 of item_type.rb which is the second line shown here "hash||=arrange(options)"
definitions from item_type.rb It would seem the error would be caused by arrange not being defined:
def self.arrange_as_array(options={}, hash=nil)
hash ||= arrange(options)
arr = []
hash.each do |node, children|
arr << node
arr += arrange_as_array(options, children) unless children.nil?
end
arr
end
def name_for_selects
"#{'-' * depth} #{name}"
end
def possible_parents
parents = ItemType.arrange_as_array(:order => 'name')
return new_record? ? parents : parents - subtree
end
This is how I'm using arrange_as_array instantiating @item_types
in my controller:
def get_item_types
#@item_types=ItemType.arrange_as_array
@item_types = ItemType.arrange_as_array({:order => 'name'})
#@item_types = ItemType.order(:names_depth_cache).map { |c| ["-" * c.depth + c.name,c.id] }
end
Have you tried something like this?
items = ItemType.all
@item_types = ItemType.sort_by_ancestry(items, &:name).map { |c| ["-" * c.depth + c.name,c.id] }
You may be able to get away without the block sort
items = ItemType.all.ordered_by_ancestry_and(:name)
@item_types = ItemType.sort_by_ancestry(items).map { |c| ["-" * c.depth + c.name,c.id] }
Try those out and see what works best for you
You also want to probably add the ItemType.all.includes(:item_property)
to remove your N+1
You haven't replied to my messages in 2 weeks.
I'm hoping that means you had success.
Best of luck