# Pseudo array for Rails migration

**URL:** <https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335>\
**Category:** Ruby\
**Created:** [November 22, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335 "2009-11-22T15:06:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 22, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335/1 "2009-11-22T15:06:34Z")

</div>

Consider

N\_X = 20 # size of pseudo array

class CreatePeople \< ActiveRecord::Migration

```
create_table :people do |t|
  t.string :first_name , :null => false
  .
  .
  .
  t.integer :x00
  t.integer :x01
  t.integer :x02
  .
  .
  .
  t.integer :x18
  t.integer :x19

  t.timestamps
end

```

end

What is the (best) way of generating the “t.integer :xYY” (where YY is  
00 through 19, inclusive) which is dependent on N\_X.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 22, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335/2 "2009-11-22T15:57:29Z")

</div>

On 22.11.2009 15:06, Ralph S. wrote:

> ```
> .
> 
> t.timestamps
> end
> 
> ```
> 
> end
> 
> What is the (best) way of generating the “t.integer :xYY” (where YY is  
> 00 through 19, inclusive) which is dependent on N\_X.

What stops you from doing

N\_X = 20 # size of pseudo array

class CreatePeople \< ActiveRecord::Migration

```
 create_table :people do |t|
   t.string :first_name , :null => false
   .
   .
   .
   N_X.times do |i|
     t.integer(("x%02d" % i).to_sym)
   end

   t.timestamps
 end

```

end

? Btw, having columns with indexes in their name is considered bad  
schema design because you either could normalize it into another table  
and join or provide more meaningful names instead of “x02” if it is used  
as a placeholder. My 0.02EUR.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 22, 2009, 4:22pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335/3 "2009-11-22T16:22:39Z")

</div>

RK\> On 22.11.2009 15:06, Ralph S. wrote:

> > ```
> > .
> > 
> > t.timestamps
> > end
> > 
> > ```
> > 
> > end
> > 
> > What is the (best) way of generating the “t.integer :xYY” (where YY is  
> > 00 through 19, inclusive) which is dependent on N\_X.

RK\> What stops you from doing

RK\> N\_X = 20 # size of pseudo array

RK\> class CreatePeople \< ActiveRecord::Migration

RK\> create\_table :people do |t|  
RK\> t.string :first\_name , :null =\> false  
RK\> .  
RK\> .  
RK\> .  
RK\> N\_X.times do |i|  
RK\> t.integer((“x%02d” % i).to\_sym)  
RK\> end

RK\> t.timestamps  
RK\> end  
RK\> end

RK\> ? Btw, having columns with indexes in their name is considered bad  
RK\> schema design because you either could normalize it into another  
table  
RK\> and join or provide more meaningful names instead of “x02” if it is  
used  
RK\> as a placeholder. My 0.02EUR.

RK\> Kind regards

First of all, I’m a noob. I should have said that.

Thus your solution  
(1) Makes sense.  
(2) Explains a lot about Ruby.

For both I thank you.

In terms of normalizing the database, I ran some timing tests and  
normalizing it slows me down a couple of orders of magnitude. I’m  
willing to live with the lack of purity for the sake of performance.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 25, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335/4 "2009-11-25T18:35:23Z")

</div>

On 22.11.2009 17:05, Marnen Laibow-Koser wrote:

> > normalizing it slows me down a couple of orders of magnitude. I’m  
> > willing to live with the lack of purity for the sake of performance.
> 
> A couple orders of magnitude? This suggests to me that your  
> normalization and/or queries are poorly done. What is chewing up so  
> much time, and what does the normalized schema look like? It should be  
> possible to find a solution that performs reasonably and doesn’t have 20  
> repeating fields.

I second that. Ralph, any information about the slowness? Is it in the  
database or outside?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 22, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335/5 "2009-11-22T17:05:23Z")

</div>

Ralph S. wrote:  
[…]

> RK\> ? Btw, having columns with indexes in their name is considered bad  
> RK\> schema design because you either could normalize it into another  
> table  
> RK\> and join or provide more meaningful names instead of “x02” if it is  
> used  
> RK\> as a placeholder. My 0.02EUR.
> 
> RK\> Kind regards
> 
> First of all, I’m a noob. I should have said that.
> 
> Thus your solution  
> (1) Makes sense.  
> (2) Explains a lot about Ruby.
> 
> For both I thank you.
> 
> In terms of normalizing the database, I ran some timing tests and  
> normalizing it slows me down a couple of orders of magnitude. I’m  
> willing to live with the lack of purity for the sake of performance.

A couple orders of magnitude? This suggests to me that your  
normalization and/or queries are poorly done. What is chewing up so  
much time, and what does the normalized schema look like? It should be  
possible to find a solution that performs reasonably and doesn’t have 20  
repeating fields.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 25, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335/6 "2009-11-25T19:05:50Z")

</div>

Wednesday, November 25, 2009, 10:35:06 AM, you wrote:

RK\> On 22.11.2009 17:05, Marnen Laibow-Koser wrote:

> > > normalizing it slows me down a couple of orders of magnitude. I’m  
> > > willing to live with the lack of purity for the sake of performance.
> > 
> > A couple orders of magnitude? This suggests to me that your  
> > normalization and/or queries are poorly done. What is chewing up so  
> > much time, and what does the normalized schema look like? It should be  
> > possible to find a solution that performs reasonably and doesn’t have 20  
> > repeating fields.

RK\> I second that. Ralph, any information about the slowness? Is it in  
the  
RK\> database or outside?

I _think_ it is, in part, coming from the logging that is being done.

Instead of one create … one can have 21.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 25, 2009, 9:42pm UTC](https://www.ruby-forum.com/t/pseudo-array-for-rails-migration/179335/7 "2009-11-25T21:42:29Z")

</div>

Ralph S. wrote:

> Wednesday, November 25, 2009, 10:35:06 AM, you wrote:
> 
> RK\> On 22.11.2009 17:05, Marnen Laibow-Koser wrote:
> 
> > > > normalizing it slows me down a couple of orders of magnitude. I’m  
> > > > willing to live with the lack of purity for the sake of performance.
> > > 
> > > A couple orders of magnitude? This suggests to me that your  
> > > normalization and/or queries are poorly done. What is chewing up so  
> > > much time, and what does the normalized schema look like? It should be  
> > > possible to find a solution that performs reasonably and doesn’t have 20  
> > > repeating fields.
> 
> RK\> I second that. Ralph, any information about the slowness? Is it in  
> the  
> RK\> database or outside?
> 
> I _think_ it is, in part, coming from the logging that is being done.
> 
> Instead of one create … one can have 21.

Then, as I suspected, you are using the DB incorrectly. To create 21  
records, use 1 query, not 21. You can use ar-extensions or something  
similar to do this with ActiveRecord.

Rule of thumb: queries don’t belong inside loops. If you find yourself  
putting a query in a loop, you have a design problem.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
