0

How do I access the relational table UserRole of the database in the rails console?

Schema:

ActiveRecord::Schema.define(version: 20140709163149) do
  create_table "users", force: true do |t|
    t.string   "stuff"
    ...
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "roles", force: true do |t|
    t.string   "label"
    ...
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "user_roles", force: true do |t|
    t.integer  "user_id"
    t.integer  "role_id"
    ...
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "user_roles", ["role_id"], name: "index_user_roles_on_role_id"
  add_index "user_roles", ["user_id"], name: "index_user_roles_on_user_id"

In the rails console, I can do User.all() and get all the users, and I can do Role.all(), but how do I access the relationship table user_role? Ultimately so I can seed the database.

If I try UserRole.all(), I get the following:

NameError: uninitialized constant UserRole
    from (irb):7
    from /Users/dejanew/.rvm/gems/ruby-2.1.1@global/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
    from /Users/dejanew/.rvm/gems/ruby-2.1.1@global/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/dejanew/.rvm/gems/ruby-2.1.1@global/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /Users/dejanew/.rvm/gems/ruby-2.1.1@global/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /Users/dejanew/.rvm/gems/ruby-2.1.1@global/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/dejanew/Dropbox/dev/SDF/bin/rails:8:in `<top (required)>'
    from /Users/dejanew/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/dejanew/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'
chris Frisina
  • 19,086
  • 22
  • 87
  • 167

3 Answers3

1

You need to define a model called UserRole in app/models/user_role.rb.

When you use User.all() and Roll.all(), you're accessing classes defined (presumably) in app/models/user.rb and app/models/role.rb respectively. You are not directly accessing the tables through some syntactic sugar available in the Rails console, you're simply using plain old classes defined by your application's code. You need to do the same for UserRole.

user229044
  • 232,980
  • 40
  • 330
  • 338
1

User and Role are not database tables. They're models, which is a word used in Rails to mean Ruby classes that inherit from ActiveRecord::Base.

You defined the User model when you typed (or generated with rails generate) class User < ActiveRecord::Base ... end in /app/models/user.rb. Rails automatically hooks the User and Role models up to the users and roles tables, but that's (for our purposes) coincidental. User and Role exist because there are classes with those names defined in /app/models, not because there are database tables called users and roles.

If you want to be have a model called UserRole, you'll have to define it. You'll do that by creating (or generating) /users/models/user_role.rb and putting class UserRole < ActiveRecord::Base ... end in it.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
1

Hopefully this is a simple solution.

Your app/models directory should have user.rb, user_role.rb, and role.rb.

Where user_role.rb looks something like this:

class UserRole < ActiveRecord::Base belongs_to :user belongs_to :role end

Also note, that the type field for Role may trigger single table inheritance.

kbrock
  • 958
  • 12
  • 15