Has_many通过多个属性?

时间:2016-07-28 06:13:04

标签: ruby-on-rails database has-many-through

我正在尝试通过一个表构建一个模型,其中用户有许多角色,如:技能,服务,项目等。

我一整天都在努力争取技能表工作,并将其建模为:

class User < ApplicationRecord
    has_many :roles
    has_many :skills, through: :roles

class Skill < ApplicationRecord
  has_many :roles
  has_many :users, through: :roles
end


class Role < ApplicationRecord
  belongs_to :user
  belongs_to :skill
end

我能得到的最接近的是这个错误。

skill.users
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) "user" or :users in model Role. Try 'has_many :users, :through => :roles, :source => <name>'. Is it one of ?

这是架构:

ActiveRecord::Schema.define(version: 20160728053925) do

  create_table "relationships", force: :cascade do |t|
    t.integer  "userSkill"
    t.integer  "skillUser"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["skillUser", "userSkill"], name: "index_relationships_on_skillUser_and_userSkill", unique: true
    t.index ["skillUser"], name: "index_relationships_on_skillUser"
    t.index ["userSkill"], name: "index_relationships_on_userSkill"
  end

  create_table "roles", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "skill_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["skill_id"], name: "index_roles_on_skill_id"
    t.index ["user_id"], name: "index_roles_on_user_id"
  end

  create_table "skills", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "firstname"
    t.string   "lastname"
    t.string   "zipcode"
    t.string   "email"
    t.datetime "created_at",                        null: false
    t.datetime "updated_at",                        null: false
    t.string   "password_digest"
    t.string   "remember_digest"
    t.boolean  "admin",             default: false
    t.string   "activation_digest"
    t.boolean  "activated",         default: false
    t.datetime "activated_at"
    t.string   "reset_digest"
    t.datetime "reset_sent_at"
    t.index ["email"], name: "index_users_on_email", unique: true
  end

end

那个关系表是从被破坏的迁移中叛逆的,并不属于那里,但我可以自己处理如何删除它。

无论如何,我错过了什么?我是否在正确的轨道上,如何通过工作获得我的has_many,这样我就可以为用户分配技能。

最终的想法是,许多用户可以拥有相同的技能..

  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
=> #<User id: 1, firstname: "Jeremy", lastname: "*****", zipcode: nil, email: "*****@gmail.com", created_at: "2016-07-28 05:40:45", updated_at: "2016-07-28 05:40:45", password_digest: "$2a$10$gGkUlUXDVrVtYzHzDdnPQenVrX8sxoXkgdIVeHxIQPr...", remember_digest: nil, admin: true, activation_digest: "$2a$10$6isSjCt3dnVDxEq4HKLBbOTkKFBOEDV8my94rdfZQJL...", activated: true, activated_at: "2016-07-28 05:40:45", reset_digest: nil, reset_sent_at: nil>
irb(main):047:0> skill = Skill.find 1
  Skill Load (0.2ms)  SELECT  "skills".* FROM "skills" WHERE "skills"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
=> #<Skill id: 1, name: "comedy", created_at: "2016-07-28 05:53:56", updated_at: "2016-07-28 05:53:56">
irb(main):048:0> user.skills
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) "skill" or :skills in model Role. Try 'has_many :skills, :through => :roles, :source => <name>'. Is it one of ?
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/reflection.rb:914:in `check_validity!'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations/association.rb:25:in `initialize'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations/has_many_through_association.rb:8:in `initialize'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations.rb:235:in `new'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations.rb:235:in `association'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations/builder/association.rb:111:in `skills'
    from (irb):48
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/console.rb:65:in `start'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/console_helper.rb:9:in `start'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `block in require'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
    from /home/jeremy/dareal/bin/rails:9:in `<top (required)>'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `block in load'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'
irb(main):049:0> skill.users
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) "user" or :users in model Role. Try 'has_many :users, :through => :roles, :source => <name>'. Is it one of ?
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/reflection.rb:914:in `check_validity!'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations/association.rb:25:in `initialize'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations/has_many_through_association.rb:8:in `initialize'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations.rb:235:in `new'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations.rb:235:in `association'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activerecord-5.0.0/lib/active_record/associations/builder/association.rb:111:in `users'
    from (irb):49
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/console.rb:65:in `start'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/console_helper.rb:9:in `start'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /home/jeremy/.gem/ruby/2.3.0/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `block in require'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
    from /home/jeremy/dareal/bin/rails:9:in `<top (required)>'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `block in load'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/jeremy/.gem/ruby/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

0 个答案:

没有答案
相关问题