修正了: 更換型號名稱相匹配的軌道命名約定我的has_and_belongs_to_many關係有什麼問題?
我收到以下錯誤,當我試圖加入一個技能的用戶:
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>
這裏是我的用戶。 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
如果你能幫助那簡直是真棒!如果您需要查看其他文件,請告訴我。
你是怎麼得到複雜的模型名稱的?這不能幫助。在任何情況下,'habtm'關係都需要`技能'技能' - 你不會爲單個技能設置一個技能列表,而是添加一項技能。不過,在這種情況下,我會對班級命名感到緊張。可能是錯的。 – 2011-12-14 19:26:37
使用由has_and_belongs_to_many宏生成的`collection <<(object,...)`方法,所以在你的情況下它會是'user.skills << skill` – maprihoda 2011-12-14 19:33:50