# Associations not saved in migration

**URL:** <https://www.ruby-forum.com/t/associations-not-saved-in-migration/150302>\
**Category:** Rails\
**Created:** [October 7, 2008, 4:14am UTC](https://www.ruby-forum.com/t/associations-not-saved-in-migration/150302 "2008-10-07T04:14:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrpete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@mrpete](https://www.ruby-forum.com/u/mrpete)\
**Post date:** [October 7, 2008, 4:14am UTC](https://www.ruby-forum.com/t/associations-not-saved-in-migration/150302/1 "2008-10-07T04:14:14Z")

</div>

hi. i’ve got a couple migrations where i’ve added foreign keys and  
tried to update them via the migration but my items don’t save. i’ve  
used save! in the hopes that an exception gets raised but no luck. the  
migrations run without errors but the data isn’t in the db. can anyone  
point out what i might be doing wrong? thanks! code below.

class AddDeliIdToMenuItem \< ActiveRecord::Migration  
def self.up  
add\_column :menu\_items, :deli\_id, :integer

```
# todo - the code below doesn't work, wtf?
deli = Deli.find(:first)
items = MenuItem.find(:all)
for item in items do
  item.deli = deli
  item.save!
end

```

end

def self.down  
remove\_column :menu\_items, :deli\_id  
end  
end

class CreateTestMenuItemTypes \< ActiveRecord::Migration  
def self.up  
type = MenuItemType.create :name =\> ‘Sandwiches’  
items = MenuItem.find\_all\_by\_item\_type “Sandwhich”  
for item in items do  
puts “item: #{item.name}, type id: #{type.id}”  
item.menu\_item\_type\_id = type.id  
puts “item: #{item.name}, type id: #{item.menu\_item\_type\_id}”  
item.save!  
end  
type = MenuItemType.create :name =\> ‘Salads’  
items = MenuItem.find\_all\_by\_item\_type “Salad”  
for item in items do  
puts “item: #{item.name}, type id: #{type.id}”  
item.menu\_item\_type\_id = type.id  
puts “item: #{item.name}, type id: #{item.menu\_item\_type\_id}”  
item.save!  
end  
type = MenuItemType.create :name =\> ‘Drinks’  
type = MenuItemType.create :name =\> ‘Smoothies’  
type = MenuItemType.create :name =\> ‘Deserts’  
type = MenuItemType.create :name =\> ‘Wraps’  
end

def self.down  
end  
end
