我的has_and_belongs_to_many关系怎么了?

时间:2011-12-14 19:23:52

标签: mysql ruby-on-rails ruby rubygems

FIXED BY: 更改模型名称以匹配rails命名约定

当我尝试将技能加入用户时,我收到以下错误:

 irb(main):006:0> user.skills << skill
NameError: uninitialized constant Users::Skill
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_r
ecord/base.rb:1341:in `compute_type'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_r
ecord/reflection.rb:173:in `klass'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_r
ecord/associations/collection_association.rb:146:in `transaction'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_r
ecord/associations/collection_association.rb:124:in `concat'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.1.3/lib/active_r
ecord/associations/collection_proxy.rb:118:in `<<'
        from (irb):6
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/comman
ds/console.rb:45:in `start'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/comman
ds/console.rb:8:in `start'
        from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/comman
ds.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
irb(main):007:0>

这是我的users.rb文件:

class Users < ActiveRecord::Base

    has_and_belongs_to_many :skills

end

这是我的skills.rb文件:

class Skills < ActiveRecord::Base

    has_and_belongs_to_many :users

end

这是我的schema.rb文件:

ActiveRecord::Schema.define(:version => 20111214152402) do

  create_table "skills", :force => true do |t|
    t.integer  "user_id"
    t.string   "description", :null => false
    t.string   "skill_name",  :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "skills", ["user_id"], :name => "index_skills_on_user_id"

  create_table "users", :force => true do |t|
    t.integer  "skill_id"
    t.string   "first_name",   :limit => 25
    t.string   "last_name",    :limit => 50
    t.string   "email",                       :default => ""
    t.string   "password",     :limit => 40
    t.string   "location",     :limit => 100
    t.string   "status"
    t.text     "bio"
    t.string   "question1"
    t.string   "question2"
    t.string   "availability"
    t.string   "image"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "users", ["skill_id"], :name => "index_users_on_skill_id"

  create_table "users_skills", :id => false, :force => true do |t|
    t.integer "user_id"
    t.integer "page_id"
  end

  add_index "users_skills", ["user_id", "page_id"], :name => "index_users_skills_on_user_id_and_page_id"

end

如果你能提供帮助那就太棒了!如果您需要查看其他文件,请告诉我。

2 个答案:

答案 0 :(得分:3)

我认为,问题是skill是Skill的一个实例,而user.skills需要一个集合。

users.skills = [skill]应该有效,但有不同的方式来创建关联。

UPD。戴夫牛顿是对的。要将记录添加到关联,您应该使用<<

答案 1 :(得分:3)

user.skills是一个数组,所以为了添加技能,你需要做

user.skills << skill

此外,在您的架构上,我注意到您的user_skils表包含错误的外键ID。我认为它们应该是user_idskill_id

<强>更新

你的users表也看错了。您的用户表中不需要skill_id。同样,您的技能表中不需要user_id。

这是表格和属性应如何显示的小图

users            skills_users         skills
id      ---->    user_id
                 skill_id        ---> id

更新2

您的型号名称错误。在RoR中使用单一模型名称是一种惯例。 Users应为User。同样,对于技能。