我有一個自聯接在我的模型:爲什麼我的has_many foreign_key協會沒有返回我期望的內容?
class Frame < ActiveRecord::Base
has_many :player_frames, class_name: 'Frame', foreign_key: 'player_id'
belongs_to :player
end
模式:
create_table "frames", force: :cascade do |t|
t.integer "number"
t.integer "player_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "players", force: :cascade do |t|
t.integer "number"
t.integer "game_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我會想象player_frames
返回所有共享相同player_id自我同行幀。但事實並非如此。
0> player_id
=> 1
0> player_frames
Frame Load (0.4ms) SELECT "frames".* FROM "frames" WHERE "frames"."player_id" = $1 [["player_id", 4]]
=> #<ActiveRecord::Associations::CollectionProxy []>
爲什麼它試圖加載player_id爲4的幀?