ich ein straight forward Assoziation zwischen 3 verschiedenen Modellen haben:Rails 5 - has_one: durch mit: class_name nicht Datensatz zugeordnet Bietet
class Claim < ApplicationRecord
has_one :private_car, class_name: 'Car'
has_one :company_car, class_name: 'Car'
has_one :user, through: :private_car
end
class Car < ApplicationRecord
belongs_to :claim
belongs_to :user
end
class User < ApplicationRecord
has_many :cars
end
Die entsprechenden Tabellen dann haben die folgenden Fremdschlüssel definiert:
Claim => ['private_car_id','company_car_id']
Car => ['user_id','claim_id']
User => none
die Konsolenausgabe ist wie folgt:
2.3.1 :007 > Claim.first.private_car
Case Load (0.4ms) SELECT "cases".* FROM "claims" WHERE "claims"."deleted_at" IS NULL ORDER BY "claims"."id" ASC LIMIT $1 [["LIMIT", 1]]
Car Load (0.2ms) SELECT "cars".* FROM "cars" WHERE "cars"."deleted_at" IS NULL AND "cars"."claim_id" = $1 LIMIT $2 [["claim_id", 1], ["LIMIT", 1]]
=> nil
2.3.1 :008 > Claim.first.private_car_id
Claim Load (0.6ms) SELECT "claims".* FROM "claims" WHERE "claims"."deleted_at" IS NULL ORDER BY "claims"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> 1
2.3.1 :009 > Claim.first.user
Claim Load (0.3ms) SELECT "claims".* FROM "claims" WHERE "claims"."deleted_at" IS NULL ORDER BY "claims"."id" ASC LIMIT $1 [["LIMIT", 1]]
User Load (0.5ms) SELECT "users".* FROM "users" INNER JOIN "cars" ON "users"."id" = "cars"."user_id" WHERE "users"."deleted_at" IS NULL AND "cars"."deleted_at" IS NULL AND "cars"."claim_id" = $1 LIMIT $2 [["claim_id", 1], ["LIMIT", 1]]
=> nil
Jede Idee, warum die associat Ion ist gebrochen?