On 18 May 2010 06:32, Ali <ali.akhtarz...@gmail.com> wrote:
> 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 => 'a...@ex.com',
>                             :profile => 'Site Administrator',
>                             :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

>    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.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to