# Why can't I save to a join table in a migration or rake tas

**URL:** <https://www.ruby-forum.com/t/why-cant-i-save-to-a-join-table-in-a-migration-or-rake-tas/103917>\
**Category:** Rails\
**Created:** [May 11, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/why-cant-i-save-to-a-join-table-in-a-migration-or-rake-tas/103917 "2007-05-11T17:21:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stepheneb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@stepheneb](https://www.ruby-forum.com/u/stepheneb)\
**Post date:** [May 11, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/why-cant-i-save-to-a-join-table-in-a-migration-or-rake-tas/103917/1 "2007-05-11T17:21:45Z")

</div>

> u.roles \<\< member\_role  
> p “#{u.roles.size}”  
> p “added member role to #{u.name}”  
> end
> 
> and see what happens

Chris,

Thanks so much for providing another set of eyes. I took your suggestion  
and expanded on it. Quickly here was the problem:

anonymous.roles.each {|r| r.destroy }

What that actually does is delete all the roles\_users associations that  
point to the any roles the anonymous user has:

This is the command that does what I intended:

anonymous.roles.delete\_all

For anybody else that finds this email in the future here’s what I put  
in the rake task to help me debug the problem:

task :setup\_roles =\> :environment do  
anonymous\_role = Role.find\_or\_create\_by\_title(‘anonymous’)  
member\_role = Role.find\_or\_create\_by\_title(‘member’)  
mananger\_role = Role.find\_or\_create\_by\_title(‘manager’)  
author\_role = Role.find\_or\_create\_by\_title(‘author’)  
admin\_role = Role.find\_or\_create\_by\_title(‘admin’)  
User.find(:all).each do |u|  
puts “\n–#{u.name}–”  
puts " roles: size: #{u.roles.size}"  
u.roles \<\< member\_role  
puts " added member role to #{u.name}: u.roles \<\< member\_role"  
puts " roles: size: :#{u.roles.size}"  
end  
puts “\n1: Checking Users for Roles:”  
User.find(:all).each do |u|  
puts “\n–#{u.name}–”  
puts " roles: size: #{u.roles.size}"  
end  
anonymous = User.find\_by\_login(‘Anonymous’)  
anonymous.roles.each {|r| r.destroy } # \*\*\* this is the problem \*\*\*  
anonymous.roles \<\< anonymous\_role  
stephen = User.find\_by\_login(‘stephen’)  
stephen.roles \<\< admin\_role  
puts “\n2: Checking Users for Roles:”  
User.find(:all).each do |u|  
puts “\n–#{u.name}–”  
puts " roles: size: #{u.roles.size}"  
end  
end

On the first loop I got output like this:

–Anonymous User–  
roles: size: 0  
added member role to Anonymous User: u.roles \<\< member\_role  
roles: size: :1

–Stephen B.–  
roles: size: 0  
added member role to Stephen B.: u.roles \<\< member\_role  
roles: size: :1

–Carolyn Staudt–  
roles: size: 0  
added member role to Carolyn Staudt: u.roles \<\< member\_role  
roles: size: :1

…

Everything still looks fine right after that:

1: Checking Users for Roles:

–Anonymous User–  
roles: size: 1

–Stephen B.–  
roles: size: 1

–Carolyn Staudt–  
roles: size: 1

…

But at the end it looks like this:

2: Checking Users for Roles:

–Anonymous User–  
roles: size: 1

–Stephen B.–  
roles: size: 1

–Carolyn Staudt–  
roles: size: 0

…

All the roles\_users associations I made for the Users are gone.
