# Codegeneration in Model slow

**URL:** <https://www.ruby-forum.com/t/codegeneration-in-model-slow/188187>\
**Category:** Rails\
**Created:** [May 7, 2010, 10:47am UTC](https://www.ruby-forum.com/t/codegeneration-in-model-slow/188187 "2010-05-07T10:47:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![inriz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/50afbb/32.png) [@inriz](https://www.ruby-forum.com/u/inriz)\
**Post date:** [May 7, 2010, 10:47am UTC](https://www.ruby-forum.com/t/codegeneration-in-model-slow/188187/1 "2010-05-07T10:47:46Z")

</div>

hello list,

i’ve got the following code snippet in my model to generate extra  
helper functions.

```
def transform_to_boolean
  Property.columns.select {|c| c.sql_type == "varchar(1)"}.each do

```

|col|  
define\_method “#{col.name.to\_s}”.to\_sym do  
v = self.attributes[col.name.to\_s]  
v = (v ? v.upcase : “F”)  
if (v == “W” || v == “T”)  
true  
else  
false  
end  
end  
end  
end

this functionality takes over 1,2 sec for one instance of the model.  
It has to iterate over 260 columns.

Do you have an idea how to speed up this piece of functionality?

best regards,

matthias

---

<div class="post-metadata">

**Author:** ![inriz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/50afbb/32.png) [@inriz](https://www.ruby-forum.com/u/inriz)\
**Post date:** [May 7, 2010, 11:55am UTC](https://www.ruby-forum.com/t/codegeneration-in-model-slow/188187/2 "2010-05-07T11:55:40Z")

</div>

On May 7, 9:46 am, inriz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> this functionality takes over 1,2 sec for one instance of the model.  
> It has to iterate over 260 columns.
> 
> Do you have an idea how to speed up this piece of functionality?

why do you need to call this on each instance ?

Fred

---

<div class="post-metadata">

**Author:** ![inriz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/50afbb/32.png) [@inriz](https://www.ruby-forum.com/u/inriz)\
**Post date:** [May 7, 2010, 11:01am UTC](https://www.ruby-forum.com/t/codegeneration-in-model-slow/188187/3 "2010-05-07T11:01:14Z")

</div>

On 7 Mai, 10:46, inriz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> v = (v ? v.upcase : "F")
> 
> ```
> 
> It has to iterate over 260 columns.  
> To unsubscribe from this group, send email to removed\_email\_address@domain.invalid.  
> For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.

this is the call of evil  
v = self.attributes[col.name.to\_s]  
i change it to  
v = self.read\_attribute(col.name.to\_sym)

its much faster.
