# Excessive use of CPU

**URL:** <https://www.ruby-forum.com/t/excessive-use-of-cpu/171747>\
**Category:** Rails\
**Created:** [July 8, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/excessive-use-of-cpu/171747 "2009-07-08T16:24:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leandro\_Maioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/db5fbb/32.png) [@Leandro\_Maioral](https://www.ruby-forum.com/u/Leandro_Maioral)\
**Post date:** [July 8, 2009, 4:24pm UTC](https://www.ruby-forum.com/t/excessive-use-of-cpu/171747/1 "2009-07-08T16:24:36Z")

</div>

Hello guys and girls, I have a problem with a method (new), it is  
using 100% of CPU for 15/20 seconds. Follow the method below:

[http://pastie.org/537568](http://pastie.org/537568)

## kits\_controller.rb

def new  
@page\_title = ‘Add a Kit’  
@kit = Kit.new

@num\_itens = 16  
@kit\_inclusions = Array.new  
@num\_itens.times { @kit\_inclusions.push(KitInclusion.new) }

```
render :action => 'form'

```

end

def new\_do  
kit = Kit.new(params[:form])

if kit.save  
params[:kit\_inclusions].each do |ki|  
next if ki[:product\_id].empty?

```
  kit_inclusion = KitInclusion.new(ki)
  kit_inclusion.kit_id = kit.id
  kit_inclusion.save
end

flash[:notice] = 'Kit added!'
redirect_to :action => 'index'

```

else  
flash[:notice] = kit.errors.full\_messages.map {|fm| “

- #{fm}\</  
li\>”}  
redirect\_to :action =\> ‘new’  
end  
end

## form.hmtl.erb

\<% form\_for :form, @kit, :url =\> { :action =\> action\_do } do |f| -%\>  
\<% @num\_itens.times do |t| -%\>  
\<% fields\_for “kit\_inclusions[]”, @kit\_inclusions[t] do |i| -%\>

- **Product \<%= t + 1 %\>:** \<%=  
i.collection\_select “product\_id”, Product.all(:conditions =\>  
{:available =\> true}, :order =\> “category\_id, model  
ASC”), :id, :display\_for\_select, {:include\_blank =\> ‘Select…’},  
{:onchange =\> “qntDefault('quantity\_#{t}');”} %\>\<%= i.text\_field  
“quantity”, {:onkeydown =\> “return noLetters(event);”, :class =\>  
‘quantity’, :id =\> “quantity\_#{t}”} %\>
  
\<% end -%\>  
\<% end -%\>  
\<%= image\_submit\_tag ‘btn\_save.png’ %\>  
\<%= f.hidden\_field :id %\>  
\<% end -%\>

---

<div class="post-metadata">

**Author:** ![Leandro\_Maioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/db5fbb/32.png) [@Leandro\_Maioral](https://www.ruby-forum.com/u/Leandro_Maioral)\
**Post date:** [July 8, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/excessive-use-of-cpu/171747/2 "2009-07-08T20:51:46Z")

</div>

Can you give more information on your environment? It shouldn’t be  
taking that long…

I’d also advise you to read more documentation; some parts of your  
code are working against the Rails default conventions (for instance:  
the action that creates records is typically ‘create’, not ‘new\_do’).  
It’s a small thing, but it means that you have to do extra typing  
(example: the :url option to form\_for).

You may also, if you’re on 2.3, want to look into the nested model  
support stuff, which will handle much of what you’re doing here.

–Matt J.

---

<div class="post-metadata">

**Author:** ![Leandro\_Maioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/db5fbb/32.png) [@Leandro\_Maioral](https://www.ruby-forum.com/u/Leandro_Maioral)\
**Post date:** [July 9, 2009, 3:33am UTC](https://www.ruby-forum.com/t/excessive-use-of-cpu/171747/3 "2009-07-09T03:33:11Z")

</div>

Hello Matt, thanks for the atention.

I’m under Ruby 1.8.6 and Rails 2.3.2. The machine is a Macbook Pro  
with a Core 2 Duo 2.4GHz and 4GB of RAM. I have declared the nested  
conditions in the model as seen bellow.

class Kit \< ActiveRecord::Base

has\_many :kit\_inclusions  
has\_many :products, :through =\> :kit\_inclusions

accepts\_nested\_attributes\_for :kit\_inclusions, :allow\_destroy =\>  
true

def before\_validation  
self.name = self.name.to\_s.strip.downcase.titleize  
self.comments = self.comments.to\_s.strip.capitalize  
end

validates\_presence\_of :name, :message =\> ‘é requerido’

def to\_s  
self.name  
end

end
