# Seed\_fu and has\_many :through

**URL:** <https://www.ruby-forum.com/t/seed-fu-and-has-many-through/185938>\
**Category:** Rails\
**Created:** [March 29, 2010, 2:22pm UTC](https://www.ruby-forum.com/t/seed-fu-and-has-many-through/185938 "2010-03-29T14:22:43Z")\
**Posts on this page:** 4\
**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 29, 2010, 2:22pm UTC](https://www.ruby-forum.com/t/seed-fu-and-has-many-through/185938/1 "2010-03-29T14:22:43Z")

</div>

Hi  
In my application to seed data I am using seed\_fu  
([GitHub - mbleigh/seed-fu: Advanced seed data handling for Rails, combining the best practices of several methods together.](http://github.com/mbleigh/seed-fu))

```
I have tables roles,privileges and permissions( It joins roles and

```

privileges). Now at application startup I have to seed these tables with  
data. But during runtime some additionals rows may be added to  
permissions. But I dont how at a later stage if I run rake db:seed\_fu  
all data that I set later to permissions may be wipeout. I dont know how  
to handle this.

As an example

# role

1 admin  
2 contact  
3 employee

# privilege

1 add\_contact  
2 edit\_contact  
3 delete\_employee

# permissions (role\_id,privilege\_id)

1 1  
1 2  
3 2 …etc

```
  Suppose this may be seeded at startup. Later additional entries

```

may go to permissions. One of my question is how can I use seed\_fu to  
fill data for permissions table? Can I do it as usual like create  
db/fixtures/permission.rb and

Permission.seed\_many(:id, :id, [  
{ :role\_id =\> 1, :privilege\_id =\> 1 },  
{ :role\_id =\> 1, :privilege\_id =\> 2 },  
{ :role\_id =\> 3, :privilege\_id =\> 2 }  
])

```
 One more question is if I follow like above and at a later stage if

```

run rake db:seed\_fu again all data that where added later to permission  
will be lost. And also how can I ensure that all the id values above  
should be what I expected. Please guide me

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 29, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/seed-fu-and-has-many-through/185938/2 "2010-03-29T15:28:19Z")

</div>

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

> Hi  
> In my application to seed data I am using seed\_fu  
> ([GitHub - mbleigh/seed-fu: Advanced seed data handling for Rails, combining the best practices of several methods together.](http://github.com/mbleigh/seed-fu))
> 
> I have tables roles,privileges and permissions( It joins roles and  
> privileges). Now at application startup I have to seed these tables with  
> data. But during runtime some additionals rows may be added to  
> permissions. But I dont how at a later stage if I run rake db:seed\_fu  
> all data that I set later to permissions may be wipeout. I dont know how  
> to handle this.

This is presumably a follow on to your previous thread. You never  
explained why you are repeatedly seeding the database, this is  
normally something that would be done only once.

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 30, 2010, 6:05am UTC](https://www.ruby-forum.com/t/seed-fu-and-has-many-through/185938/3 "2010-03-30T06:05:16Z")

</div>

Hi Colin

```
Suppose I have the development phases like phase1,phase2, etc. So 

```

for example in Phase1 I have to seed data to say table1 (rake  
db:seed\_fu) And it is deployed.  
Now again in phase2 again I have to seed data to table2 . Then also to  
sseed data I run  
rake db:seed\_fu

```
    But when ever I run this command it seeds data to table1 (once 

```

more) and table2 (for the first time). Is n’t it? I think I can answer  
your question. But could you please give answer to my original questions  
posted in this thread.

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 30, 2010, 9:44am UTC](https://www.ruby-forum.com/t/seed-fu-and-has-many-through/185938/4 "2010-03-30T09:44:35Z")

</div>

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

> more) and table2 (for the first time). Is n’t it? I think I can answer  
> your question. But could you please give answer to my original questions  
> posted in this thread.

Remove the data for the first table from the seed code once you have  
done it, so that the next time it just seeds the second one. You can  
always get the old version back from your version control system if  
you ever need it again.

Colin
