簡體   English   中英

我的has_and_belongs_to_many關系有什么問題?

[英]Whats wrong with my has_and_belongs_to_many relationships?

修正:更改模型名稱以匹配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

這是我的skill.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

如果您可以幫助,那就太好了! 如果您需要查看其他文件,請告訴我。

我認為,問題在於該skill是Skill的一個實例,而user.skills需要一個集合。

users.skills = [skill]應該有效,但是創建關聯的方式有很多。

更新。 戴夫·牛頓是對的。 要將記錄添加到關聯中,應使用<<

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 同樣,對於技能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM