Ich habe zwei Modelle namens Benutzer und Beziehungen. Relation speichert die Beziehungen zwischen Benutzern, und es gibt verschiedene Arten von Beziehungen. Im Moment versuche ich, zwei Arten zu implementieren: Freunde und Freundesanfragen.
Hier sind meine Klassenverknüpfungen:
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
Im IRB versuche ich, das durch User.friends generierte Array zu verwenden, und dieser Fehler wird ausgelöst:
Lade Entwicklungsumgebung (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]]
=> #
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 => '. Is it one of :owner?
<...stack trace...>
irb(main):003:0>
Mache ich etwas falsch?
Mein Schema, wie angefordert:
ActiveRecord::Schema.define(version: 20131102102750) do
# Diese Erweiterungen müssen aktiviert sein, um diese Datenbank zu unterstützen
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