3

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

这是我的班级协会:

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
4

1 回答 1

4

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

owner     => owner_id
character => character_id

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

owner     => user_owner
character => user_character

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

belongs_to :user_character, foreign_key: "character"

现在到你的主要问题。如果我理解正确你想获取字符列表,使用用户中定义的朋友关联。在这种情况下,该行应如下所示:

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"
于 2013-11-02T12:27:31.667 回答