我有两个模型,称为用户和关系。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