Ruby on rails Rails:ActiveRecord::AssociationTypeMismatch团队()应为,获取字符串()
我在试图解决代码中用户和团队的“鸡和蛋”问题时遇到了这个错误。我浏览过互联网,这似乎是一个常见的错误,考虑到我的情况,没有一个解决方案是有意义的 最低可行示例: 在rails控制台上,我创建了一个新(无效)用户: 那么一个有效的团队:Ruby on rails Rails:ActiveRecord::AssociationTypeMismatch团队()应为,获取字符串(),ruby-on-rails,ruby,activerecord,ruby-on-rails-5,Ruby On Rails,Ruby,Activerecord,Ruby On Rails 5,我在试图解决代码中用户和团队的“鸡和蛋”问题时遇到了这个错误。我浏览过互联网,这似乎是一个常见的错误,考虑到我的情况,没有一个解决方案是有意义的 最低可行示例: 在rails控制台上,我创建了一个新(无效)用户: 那么一个有效的团队: team = Team.new( name: "Test team, please ignore", tier: 0, owner: user.id, users: user.id) 获取ID: id = team.save 然后尝试创建一个有效的用户: us
team = Team.new( name: "Test team, please ignore", tier: 0, owner: user.id, users: user.id)
获取ID:
id = team.save
然后尝试创建一个有效的用户:
user = User.new(name: "sudo", email: "foo@example.com")
user = User.new(name: "sudo", email: "foo@example.com", team: id)
这些命令的输出分别为:
=> #<User id: nil, name: "sudo", email: "foo@example.com", team_id: nil, created_at: nil, updated_at: nil>
=> #<Team id: nil, name: "Test team, please ignore", tier: 0, owner: nil, users: nil, created_at: nil, updated_at: nil>
ActiveRecord::AssociationTypeMismatch: Team(#47180498893940) expected, got TrueClass(#47180477068240)
模型:
小组:
如果我能提供更多信息,请告诉我。我在rails应用程序开发方面缺乏经验。使用
user = User.new(name: "sudo", email: "foo@example.com", team: team)
由于在id=team.save
中,它将存储true
或false
。根据代码是否成功保存对象或是否发生任何错误(如验证错误),它将分别返回true
或false
如果要使用ID保存,请尝试以下操作:
team = Team.new(...) # create team with right attributes inside `new`
team.save
user = User.new(name: "sudo", email: "foo@example.com", team_id: team.id) # notice the `team_id`
由于将按id保存,因此应直接使用forign_键列名。否则,如果您使用
team
属性,那么您必须提供team
对象,而不是像id(例如21)这样的整数对象。Wow,我真的是想得太多了吗?既然你这么快就开始抽签了,介意帮我再做一件事吗?(我会在9分钟后将您标记为答案)name错误:未初始化的常量团队::所有者
我如何解决这个鸡和蛋的问题?没有团队我无法创建用户,没有所有者我无法创建团队。有“空”uuid这样的东西吗?你没有发布你的所有者
模型,但我想在创建所有者之前应该不会有任何问题,因为你的团队有一个所有者不属于协会。我猜你发的错误来自其他地方。可能需要更多关于结构和型号代码的详细信息。关于null“uuid”,我不太清楚,但您可以在配置中禁用对所属
中存在的验证。“所有者”只是一个用户,应该设置为团队成员的id。(我还没有实现这个验证)所以所有者模型只是用户模型。我发布的错误来自我上面发布的“测试”。您尚未定义所有者类或将其正确关联,这就是它抛出错误的原因。如果所有者只是一个用户,那么在团队类中,可以尝试类似于has\u one:Owner,class\u name:“user”
。欢迎您随时向我或整个SO社区询问。每个人都愿意帮忙。
class User < ApplicationRecord
belongs_to:team
end
ActiveRecord::Schema.define(version: 20170213070551) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "uuid-ossp"
create_table "teams", id: :uuid, default: -> { "uuid_generate_v4()" }, force: :cascade do |t|
t.string "name"
t.integer "tier"
t.string "owner"
t.string "users"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", id: :uuid, default: -> { "uuid_generate_v4()" }, force: :cascade do |t|
t.string "name"
t.string "email"
t.string "team_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
user = User.new(name: "sudo", email: "foo@example.com", team: team)
team = Team.new(...) # create team with right attributes inside `new`
team.save
user = User.new(name: "sudo", email: "foo@example.com", team_id: team.id) # notice the `team_id`