Question

After bundle installing the gem(gem 'rolify'), I have generated the file using the following command,

rails g rolify Role User

Below files got created,

invoke  active_record
create    app/models/role.rb
invoke    rspec
create      spec/models/role_spec.rb
invoke      factory_girl
create        spec/factories/roles.rb
insert    app/models/role.rb
create    db/migrate/20140425070708_rolify_create_roles
insert  app/models/user.rb
create  config/initializers/rolify.rb

Then, i gave

rake db:migrate

Its giving me the warning,

[WARN] table 'Role' doesn't exist. Did you run the migration ? Ignoring rolify config.

Also tables are not getting created. What is the problem am i missing anything here.? This my migration file,

 class RolifyCreateRoles < ActiveRecord::Migration
    def change
     create_table(:roles) do |t|
      t.string :name
      t.references :resource, :polymorphic => true
      t.timestamps
     end

    create_table(:users_roles, :id => false) do |t|
      t.references :user
      t.references :role
    end

    add_index(:roles, :name)
    add_index(:roles, [ :name, :resource_type, :resource_id ])
    add_index(:users_roles, [ :user_id, :role_id ])
  end
 end

My versions,

Rails - 4.1.0 Ruby - 2.1.1

Please any one help me in this..

Thanks in advance.

Was it helpful?

Solution

It's a known bug with rolify; the migration is created without the .rb extension so the rake db:migrate doesn't pick it up.

Manually rename your migration to add the .rb extension. Change:

db/migrate/20140425070708_rolify_create_roles

to be:

db/migrate/20140425070708_rolify_create_roles.rb

And then rake db:migrate again.

OTHER TIPS

I had the same problem with Rails 5.2.5 and Rolify 5.0.0 and Devise 3.5.3

ActiveRecord::StatementInvalid: Mysql2::Error: Table 'papi_development.roles' doesn't exist: SELECT `roles`.* FROM `roles`

I fixed it by commenting out my Devise line in the User model before running the rake db:migrate command.

class User < ActiveRecord::Base
  ...
  # devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  ...
end

After the successful migration, i uncommented the devise line and everything seemed to work fine from that point on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top