gpt4 book ai didi

mysql - 我的 has_and_belongs_to_many 关系有什么问题?

转载 作者:太空宇宙 更新时间:2023-11-03 17:26:25 26 4
gpt4 key购买 nike

修复者:更改模型名称以匹配 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

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

最佳答案

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 .同样,对于技能。

关于mysql - 我的 has_and_belongs_to_many 关系有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8510433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com