# Migration not creating new user object

**URL:** <https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770>\
**Category:** Rails\
**Created:** [May 18, 2010, 9:26am UTC](https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770 "2010-05-18T09:26:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [May 18, 2010, 9:26am UTC](https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770/1 "2010-05-18T09:26:59Z")

</div>

Hi, so I’m new to rails development, and I’m following a book called  
Practical Rails: Social Networking Sites. There’s a user role system  
that the book develops, each user has a one or more roles and vice  
versa. So I have the tables users, roles and roles\_users (which  
connects users to roles and vice versa).

In the Role model I have has\_and\_belongs\_to\_many :users and in the  
User model I have has\_and\_belongs\_to\_many: roles.

To get the joint table I ran the command: script/generate migration  
CreateRolesUsersJoin and in the 2010301\_create\_roles\_users\_join.rb  
file I have the following:

def self.up  
create\_table :roles\_users, :id =\> false do |t|  
t.column :role\_id, :integer, :null =\> false  
t.column :user\_id, :integer, :null =\> false  
end  
admin\_user = User.create(:username =\> ‘Admin’,  
:email =\> ‘removed\_email\_address@domain.invalid’,  
:profile =\> ‘Site Administrator’,  
:pwd =\> ‘admin’,  
:pwd\_confirmation =\> ‘admin’)

```
admin_role = Role.find_by_name('Administrator')
admin_user.roles << admin_role

```

end  
end

so after that I run db:migrate and I expect that a new user called  
Admin should be created int eh database and the roles\_users database  
should have one record in it connecting user Admin and role  
Administrator… but my databases are empty.

Any suggestion? I’m using rails 2.3.5 and I think the book uses an  
older version so I suspect something to do with a version difference,  
but not sure.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [May 18, 2010, 10:19am UTC](https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770/2 "2010-05-18T10:19:06Z")

</div>

On 18 May 2010 06:32, Ali [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> CreateRolesUsersJoin and in the 2010301\_create\_roles\_users\_join.rb  
> :pwd =\> ‘admin’,  
> :pwd\_confirmation =\> ‘admin’)

What happens if you type this into the ruby console (ruby  
script/console)? Does it create a user? Perhaps the validations on  
User are failing.

> admin\_role = Role.find\_by\_name(‘Administrator’)

The line above is looking for a Role ‘Administrator’ in the roles  
table. Is there such a record in the roles table?

Colin

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [May 18, 2010, 11:03am UTC](https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770/3 "2010-05-18T11:03:00Z")

</div>

Hi, ok so it’s fixed now and I have no idea how. I don’t know what the  
console output was the first time I ran the db:migrate command. I tried  
running it quite a few times after that and it wouldn’t output anything  
new.  
Anyway, I ran db:reset and everything work now.

Just to answer the other questions, yes the Administrator role was being  
created properly and User validations were not failing. Something else  
was  
up I guess. Maybe join tables have to be created at the same time? I had  
the  
users table set up a while ago…

Cheers,

- Ali

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [May 19, 2010, 5:21am UTC](https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770/4 "2010-05-19T05:21:28Z")

</div>

Hi Rob,

That would make more sense creating the Administrator at the time of the  
users migration, but I’m following a book and the roles were created  
later  
so I suppose the author just figured he’d throw it in there or  
something. I  
do not have logic that prevents the admin from being deleted at this  
point  
either. Noted 🙂

Thanks.

Is it standard practice to have the classes (Role and User in this case)  
defined inside the migration RolesUsersJoin, wouldn’t it be inconvenient  
since I would have my Role and User model defined somewhere else anyway?  
Or  
are you saying I should not define a User and Role elsewhere but just  
have  
them inside the RolesUsersJoin class?

- Ali

On Wed, May 19, 2010 at 12:35 AM, Rob B.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [May 18, 2010, 2:37pm UTC](https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770/5 "2010-05-18T14:37:50Z")

</div>

On May 18, 2010, at 1:32 AM, Ali wrote:

> CreateRolesUsersJoin and in the 2010301\_create\_roles\_users\_join.rb  
> file I have the following:

class CreateRolesUsersJoin \< ActiveRecord::Migration  
class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many: roles  
# include code as needed (like a before\_save :encrypt\_password)  
end  
class Role \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

> def self.up  
> create\_table :roles\_users, :id =\> false do |t|  
> t.column :role\_id, :integer, :null =\> false  
> t.column :user\_id, :integer, :null =\> false  
> end

```
 User.reset_column_information
 Role.reset_column_information

```

> admin\_user = User.create(:username =\> ‘Admin’,  
> :email =\> ‘removed\_email\_address@domain.invalid’,  
> :profile =\> ‘Site Administrator’,  
> :pwd =\> ‘admin’,  
> :pwd\_confirmation =\> ‘admin’)
> 
> admin\_role = Role.find\_by\_name(‘Administrator’)  
> admin\_user.roles \<\< admin\_role  
> end  
> end

end

> Thanks in advance.  
> Before you use a Model inside a migration, it’s a good idea to create  
> a special class for it _inside_ the migration (which is why I added  
> the class around your code). These models should have just enough  
> content for the purpose of the migration. I’m not sure about the  
> HABTM relationships, but I threw .reset\_column\_information calls in  
> there for both models since the roles\_users table is new and common to  
> both.

One question though, why didn’t you create the Admin user when the  
User table was created? Do you have logic to keep the last user with  
an ‘Administrator’ role from being deleted?

-Rob

Rob B.  
[http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

> **[Launch Scout Custom Software Development & Software Consulting](https://launchscout.com/)**
>
> We solve digital problems through custom software development and design for the web—apps, mobile, desktop and devices.

removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8c91f0/32.png) [@ali](https://www.ruby-forum.com/u/ali)\
**Post date:** [May 19, 2010, 6:42am UTC](https://www.ruby-forum.com/t/migration-not-creating-new-user-object/188770/6 "2010-05-19T06:42:15Z")

</div>

On May 18, 2010, at 11:19 PM, Ali A. wrote:

> Is it standard practice to have the classes (Role and User in this  
> case) defined inside the migration RolesUsersJoin, wouldn’t it be  
> inconvenient since I would have my Role and User model defined  
> somewhere else anyway? Or are you saying I should not define a User  
> and Role elsewhere but just have them inside the RolesUsersJoin class?
> 
> - Ali

You want the normal models defined in their own files as is the  
convention. However, if you plan to manipulate the data through a  
model during a migration, then having a “local” version of that model  
defined as a nested class will shield you from problems later if the  
normal model evolves to have columns not present at the time of the  
migration that does the manipulation. (Adding a column to the table  
and then including a validation in the model is one way to do it and  
if you’re making small migrations, it’s not too hard to have a set of  
migrations that are deployed to production at the same time that are  
“incompatible” with each other.)

Some will claim that this is being paranoid, but I’ve helped at least  
two other projects (not my own) when they encountered just such crises.

If you are only doing “migration” things (i.e., Data Definition  
Language stuff in SQL-speak), then there’s no need for a nested class.

-Rob

> has\_and\_belongs\_to\_many: roles  
> t.column :role\_id, :integer, :null =\> false  
> :pwd =\> ‘admin’,  
> so after that I run db:migrate and I expect that a new user called
> 
> an ‘Administrator’ role from being deleted?
> 
> .  
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid  
> +1 513-295-4739  
> Skype: rob.biedenharn
