# Seed data

**URL:** <https://www.ruby-forum.com/t/seed-data/185798>\
**Category:** Rails\
**Created:** [March 26, 2010, 11:19am UTC](https://www.ruby-forum.com/t/seed-data/185798 "2010-03-26T11:19:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 26, 2010, 11:19am UTC](https://www.ruby-forum.com/t/seed-data/185798/1 "2010-03-26T11:19:06Z")

</div>

Hi  
I am using seed\_fu to seed initial data to tables  
([GitHub - mbleigh/seed-fu: Advanced seed data handling for Rails, combining the best practices of several methods together.](http://github.com/mbleigh/seed-fu)) . And in db/fixtures/state.rb

I have values like (I am filling only two data for simplicity)

State.seed\_many(:name, :abbr,:fips,:country\_id, [  
{ :name =\> “Alberta”, :abbr =\> ‘AB’,:fips =\> ‘01’, :country\_id =\>  
2 },  
{ :name =\> “Alaska”, :abbr =\> ‘AK’,:fips =\> ‘02’, :country\_id =\> 1  
}  
])

It works and when I do select \* from states; I get result as

±----±-----±----------------------±-----±-----------+  
| id | abbr | name | fips | country\_id |  
±----±-----±----------------------±-----±-----------+  
| 1 | AB | Alberta | 01 | 2 |  
| 2 | AK | Alaska | 02 | 1 |

```
But now I modified state.rb as

```

State.seed\_many(:name, :abbr,:fips,:country\_id, [  
{ :name =\> “Alberta”, :abbr =\> ‘AB’,:fips =\> ‘01’, :country\_id =\>  
2 },  
{ :name =\> “Alaska”, :abbr =\> ‘AK’,:fips =\> ‘02’, :country\_id =\> 1  
}  
])  
State.destroy\_all  
State.create(:id =\> 1,:name =\> “Alabama”, :abbr =\> ‘AL’,:fips =\> ‘01’,  
:country\_id =\> 1)

And now when I do select \* from states; I get unexpected result as

±----±-----±----------------------±-----±-----------+  
| id | abbr | name | fips | country\_id |  
±----±-----±----------------------±-----±-----------+  
| 5 | AL | Alabama | 01 | 1 |

```
   What I expect was id=1 But I got id as 5 . Why this ?

```

Thanks in advance  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 26, 2010, 11:41am UTC](https://www.ruby-forum.com/t/seed-data/185798/2 "2010-03-26T11:41:43Z")

</div>

On 26 March 2010 10:19, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }  
> But now I modified state.rb as
> 
> And now when I do select \* from states; I get unexpected result as
> 
> ±----±-----±----------------------±-----±-----------+  
> | id | abbr | name | fips | country\_id |  
> ±----±-----±----------------------±-----±-----------+  
> | 5 | AL | Alabama | 01 | 1 |
> 
> ```
> What I expect was id=1 But I got id as 5 . Why this ?
> 
> ```

I _think_, though I may be corrected, that when you do create it  
allows the database to set the id. If you provide one yourself it is  
ignored. The database has allocated the next in sequence even though  
earlier ones have been removed.

Colin

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 26, 2010, 2:18pm UTC](https://www.ruby-forum.com/t/seed-data/185798/3 "2010-03-26T14:18:34Z")

</div>

hi, can u fill the database with the current\_user?

i want / need to fill a few tables after the user got activated, but  
the  
tables i need to fill have to have the user\_id in it…is that possible?

thx

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 26, 2010, 2:22pm UTC](https://www.ruby-forum.com/t/seed-data/185798/4 "2010-03-26T14:22:39Z")

</div>

On 26 March 2010 13:16, tom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi, can u fill the database with the current\_user?
> 
> i want / need to fill a few tables after the user got activated, but the  
> tables i need to fill have to have the user\_id in it…is that possible?

I don’t understand what you mean. Setting a field user\_id is not a  
problem, it is just the primary key id field that is a problem.

Colin

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 29, 2010, 6:42am UTC](https://www.ruby-forum.com/t/seed-data/185798/5 "2010-03-29T06:42:14Z")

</div>

Hi  
So my question is how can I ensure that even after running rake  
db:seed (rake db:seed\_fu) a number of times my primary key ie, id here,  
does not get modified? Should I need to write an update\_id function  
again?

Thanks  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 29, 2010, 8:00am UTC](https://www.ruby-forum.com/t/seed-data/185798/6 "2010-03-29T08:00:31Z")

</div>

On Fri, Mar 26, 2010 at 3:19 AM, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }  
> But now I modified state.rb as

Tom, you shouldn’t be setting the value of the :id field because the  
database  
will ignore your setting and assign the next unique identifier  
automatically.  
Could you explain what you’re trying to do in a step by step scenario?  
This  
may helper others on the mailing list to better assist you.

-Conrad

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 29, 2010, 10:04am UTC](https://www.ruby-forum.com/t/seed-data/185798/7 "2010-03-29T10:04:02Z")

</div>

On 29 March 2010 05:42, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi  
> So my question is how can I ensure that even after running rake  
> db:seed (rake db:seed\_fu) a number of times my primary key ie, id here,  
> does not get modified? Should I need to write an update\_id function  
> again?

If you really need the id to be pre-defined (though why you need to do  
this I do not know, it is generally a bad idea) then in your seed code  
you could check whether a record of that id already exists and if it  
does then update it rather than re-creating it.

Colin

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [March 29, 2010, 10:25am UTC](https://www.ruby-forum.com/t/seed-data/185798/8 "2010-03-29T10:25:40Z")

</div>

Hi  
Thanks for your reply

> Could you explain what you’re trying to do in a step by step scenario?  
> This  
> may helper others on the mailing list to better assist you.

What I tried is as my first post. Now just as a test a I did like

State.destroy\_all  
State.create(:id =\> 1,:name =\> “Alabama”, :abbr =\> ‘AL’,:fips =\> ‘01’,  
:country\_id =\> 1)

But after this step whenever I do rake db:seed\_fu

I dont get what I expected for the id fields. I expect id to be  
generated like  
1,2,3…

> If you really need the id to be pre-defined (though why you need to do  
> this I do not know, it is generally a bad idea)

```
  Why I need the id to be predefined means suppose at a later stage 

```

if already that id may e saved to a number of tables as foreign key. But  
suppose if execute rake db:seed\_fu then if the id changed to 5,6,7  
instead of 1,2,3  
the total solution may be a big fail  
So when I checked the way which I mentioned in my first post  
accidently I noticed this. Then post a message to a get a clarification  
since I dont know where the fault is

```
     I think one more thing to ensure the ids are the values that I 

```

expected is, after creation call a function like

```
  @country.update_id_to(1) .....

```

Thanks  
Tom
