gpt4 book ai didi

ruby-on-rails - 找不到源关联 :character in model Relation

转载 作者:行者123 更新时间:2023-11-29 11:43:25 24 4
gpt4 key购买 nike

我有两个模型,分别称为用户和关系。 Relation存储用户之间的关系,有不同类型的关系。目前,我正在尝试实现两种类型:好友和好友请求。

这是我的类(class)协会:

class Relation < ActiveRecord::Base
belongs_to :owner, class_name: "User"
validates :owner, presence: true
validates :character, presence: true
end

class User < ActiveRecord::Base

has_many :characters, dependent: :destroy
has_many :relations, foreign_key: "owner", dependent: :destroy
has_many :friends, -> { where reltype: "friend" }, through: :relations, source: "character"

has_many :reverse_relations, foreign_key: "character", class_name: "Relation", dependent: :destroy
has_many :friend_requests, -> { where reltype: "freq" }, through: :reverse_relations, source: :owner

.
.
.
end

在 IRB 中,我尝试使用应该生成的 User.friends 数组,但抛出了这个错误:

Loading development environment (Rails 4.0.0)
irb(main):001:0> user = User.find(1)
User Load (12.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
=> #<User id: 1, name: "melv", email: "melv@mx.org", password_digest: "$2a$10$g3FjyVP9ya/L.j1iWAzYH.YFOjYOyxUGp3KIt6ajic
Jf...", verified: nil, reg_ip: nil, last_ip: nil, character_limit: 3, characters: nil, created_at: "2013-10-31 19:10:36"
, updated_at: "2013-11-02 11:05:14", remember_token: "4e883d6ec84b5c142882cc084c14bc101a06350f", playchar: 0>
irb(main):002:0> user.friends
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :character in model
Relation. Try 'has_many :friends, :through => :relations, :source => <name>'. Is it one of :owner?
<...stack trace...>
irb(main):003:0>

我做错了什么吗?


我的架构,按要求:

ActiveRecord::Schema.define(version: 20131102102750) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "characters", force: true do |t|
t.integer "accountid"
t.string "username"
t.integer "roundles"
t.integer "gems"
t.integer "rank"
t.integer "tier"
t.datetime "tiertime"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "permission_ranks", force: true do |t|
t.string "label"
t.integer "badge"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "relations", force: true do |t|
t.integer "owner"
t.integer "character"
t.string "reltype"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "relations", ["character"], name: "index_relations_on_character", using: :btree
add_index "relations", ["owner", "character"], name: "index_relations_on_owner_and_character", unique: true, using: :btree
add_index "relations", ["owner"], name: "index_relations_on_owner", using: :btree

create_table "tickets", force: true do |t|
t.string "title"
t.text "desc"
t.integer "sender"
t.integer "assigned"
t.text "addinfo"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.string "password_digest"
t.boolean "verified"
t.string "reg_ip"
t.string "last_ip"
t.integer "character_limit"
t.integer "characters"
t.datetime "created_at"
t.datetime "updated_at"
t.string "remember_token"
t.integer "playchar"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["name"], name: "index_users_on_name", unique: true, using: :btree
add_index "users", ["remember_token"], name: "index_users_on_remember_token", using: :btree

end

最佳答案

好的,我认为问题出在命名上。用相同的名称命名关联和列不是一个好主意。如果可能的话,从重命名关系列开始:

owner     => owner_id
character => character_id

或者,您可以重命名关联:

owner     => user_owner
character => user_character

我看到的另一件事是您的单独字符表。你似乎想从你的关系模型中引用字符表(如果我错了请纠正我),但永远不要这样做。

belongs_to :user_character, foreign_key: "character"

现在回答您的主要问题。如果我理解正确你想获得字符列表,使用用户中定义的 friend 协会。在这种情况下,该行应如下所示:

has_many :friends, -> { where reltype: "friend" }, through: :relations, source: "user_character", class_name: "Character"

已更新

作者解释说他真的想通过friends关联来引用User模型。

# relation.rb
belongs_to :user_character, foreign_key: "character", class_name: "User"

# user.rb
has_many :friends, -> { where reltype: "friend" }, through: :relations, source: "user_character"

更新 2

关联条件有问题,在特殊情况下您需要明确指定表:

has_many :friends, -> { where("relations.reltype" => "friend") }, through: :relations, source: "user_character"

关于ruby-on-rails - 找不到源关联 :character in model Relation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19741715/

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