Migration not creating new user object

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 => ‘[email protected]’,
: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.

On 18 May 2010 06:32, Ali [email protected] 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

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

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 :slight_smile:

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.
<[email protected]

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 => ‘[email protected]’,
: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
[email protected]

[email protected]

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
[email protected]
+1 513-295-4739
Skype: rob.biedenharn