# Data migration using find\_by\_name

**URL:** <https://www.ruby-forum.com/t/data-migration-using-find-by-name/171364>\
**Category:** Rails\
**Created:** [July 1, 2009, 6:23am UTC](https://www.ruby-forum.com/t/data-migration-using-find-by-name/171364 "2009-07-01T06:23:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmht81](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/57b2e6/32.png) [@dmht81](https://www.ruby-forum.com/u/dmht81)\
**Post date:** [July 1, 2009, 6:23am UTC](https://www.ruby-forum.com/t/data-migration-using-find-by-name/171364/1 "2009-07-01T06:23:46Z")

</div>

I am trying to set an attribute of a model based on a find\_by\_name  
search but it will always return the ID of 1. When i run this same  
search in the console it returns me the correct ID (6)

class LoadDefaultUser \< ActiveRecord::Migration  
def self.up  
#department = Department.find\_by\_name(“All Departments”) \<-- this  
always returns ID of 1 for the migration. Returns ID of 6 in console  
#department = Department.find\_by\_id(6) \<-- this always returns ID  
of 1 for the migration. Returns ID of 6 in console  
department = 6 # this sets the department\_id correctly so i know  
it should work???

```
user = User.create(
  :name => 'Administrator',
  :department_id => department);
user.save!

```

end

def self.down  
end  
end
