找不到源关联:模型中的字符关系

时间:2013-11-02 12:06:47

标签: ruby-on-rails postgresql activerecord

我有两个名为User and Relations的模型。 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

1 个答案:

答案 0 :(得分:4)

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

owner     => owner_id
character => character_id

或者您可以重命名关联:

owner     => user_owner
character => user_character

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

belongs_to :user_character, foreign_key: "character"

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

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

<强>已更新

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

# 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"

<强> UPDATE2

关联条件存在问题,特别是您需要明确指定表:

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