2016-11-18 4 views
0

Ich habe einen ziemlich komplexen Modellaufbau:Schienen 4 „gekettet“ has_one: durch Beziehungen mit beharrten arbeiten, aber keine neue Objekte

class LineItem < ActiveRecord::Base # STI parent class 
end 

class VendorLineItem < LineItem 
    belongs_to: :unit_rate, class_name: 'Billing::UnitRate' 
    has_one :contract_resource, through: :unit_rate 
    has_one :resource, through: :contract_resource 
end 

Beachten Sie, dass die beide oben genannten Klassen werden nicht Namensraum, während der dazugehörigen Modelle unten Alle leben in einem Billing Namensraum.

class Billing::UnitRate < ActiveRecord::Base 
    belongs_to :contract_resource, class_name: 'Billing::ContractResource' 
    has_one :contract, through: :contract_resource 
    has_one :resource, through: :contract_resource 
    has_many :vendor_line_items, dependent: :destroy 
end 

class Billing::ContractResource < ActiveRecord::Base 
    belongs_to :contract 
    belongs_to :resource, class_name: 'Billing::Resource' 
    has_many :unit_rates, dependent: :destroy 
end 

class Billing::Resource < ActiveRecord::Base 
    has_many :contract_resources 
    has_many :contracts, through: :contract_resources 
    has_many :unit_rates, through: :contract_resources 
end 

Mein Problem ist mit der VendorLineItem Klasse, wo ich an den zugehörigen resource erhalten müssen.

Wenn ich mit persistenten Daten arbeite, ist alles in Ordnung. In einer Schienen-Konsole:

2.2.3 :001 > x = VendorLineItem.last 
    VendorLineItem Load (9.8ms) SELECT "line_items".* FROM "line_items" WHERE "line_items"."type" IN ('VendorLineItem') ORDER BY "line_items"."id" DESC LIMIT 1 
=> #<VendorLineItem id: 42, fire_id: 938774, type: "VendorLineItem", unit_rate_id: 716, units_utilized: #<BigDecimal:6184d30,'0.1E1',9(18)>, fire_department_id: nil, dollar_amount: nil, created_at: "2016-11-18 16:22:23", updated_at: "2016-11-18 16:22:23", total: #<BigDecimal:6184060,'0.25E2',9(18)>> 
2.2.3 :002 > x.resource 
    Billing::Resource Load (8.9ms) SELECT "billing"."resources".* FROM "billing"."resources" INNER JOIN "contracts_resources" ON "billing"."resources"."id" = "contracts_resources"."resource_id" INNER JOIN "billing"."unit_rates" ON "contracts_resources"."id" = "billing"."unit_rates"."contract_resource_id" WHERE "billing"."unit_rates"."id" = $1 LIMIT 1 [["id", 716]] 
=> #<Billing::Resource id: 39, name: nil, description: nil, code: nil, sort_order: nil, is_active: true, constant_name: "E0", created_at: "2015-10-14 15:59:00", updated_at: "2015-10-14 15:59:00", vendor_id: 1, resource_type_id: 37> 

Wenn OTOH, ich mit einer neuen Instanz arbeite, kommt die resource zurück als null (Schienen versuchen nicht einmal, keine SQL ausgegeben wird).

2.2.3 :003 > vli = VendorLineItem.new unit_rate_id: 711 
=> #<VendorLineItem id: nil, fire_id: nil, type: "VendorLineItem", unit_rate_id: 711, units_utilized: nil, fire_department_id: nil, dollar_amount: nil, created_at: nil, updated_at: nil, total: nil> 
2.2.3 :004 > vli.resource 
=> nil 

Aber ich die Ressource durch eine explizite Kette der Verbände zugreifen:

2.2.3 :005 > vli.unit_rate.contract_resource.resource 
    Billing::UnitRate Load (8.9ms) SELECT "billing"."unit_rates".* FROM "billing"."unit_rates" WHERE "billing"."unit_rates"."id" = $1 LIMIT 1 [["id", 711]] 
    Billing::ContractResource Load (8.8ms) SELECT "contracts_resources".* FROM "contracts_resources" WHERE "contracts_resources"."id" = $1 LIMIT 1 [["id", 261]] 
    Billing::Resource Load (8.7ms) SELECT "billing"."resources".* FROM "billing"."resources" WHERE "billing"."resources"."id" = $1 LIMIT 1 [["id", 34]] 
=> #<Billing::Resource id: 34, name: nil, description: nil, code: nil, sort_order: nil, is_active: true, constant_name: "D5", created_at: "2015-10-14 15:59:00", updated_at: "2015-10-14 15:59:00", vendor_id: 1, resource_type_id: 19> 

ich auch die Ressource durch die has_one :contract_resource, through: :unit_rate Vereinigung ohne ein Problem finden:

2.2.3 :007 > vli.contract_resource.resource 
    Billing::ContractResource Load (8.7ms) SELECT "contracts_resources".* FROM "contracts_resources" INNER JOIN "billing"."unit_rates" ON "contracts_resources"."id" = "billing"."unit_rates"."contract_resource_id" WHERE "billing"."unit_rates"."id" = $1 LIMIT 1 [["id", 711]] 
    Billing::Resource Load (8.7ms) SELECT "billing"."resources".* FROM "billing"."resources" WHERE "billing"."resources"."id" = $1 LIMIT 1 [["id", 34]] 
=> #<Billing::Resource id: 34, name: nil, description: nil, code: nil, sort_order: nil, is_active: true, constant_name: "D5", created_at: "2015-10-14 15:59:00", updated_at: "2015-10-14 15:59:00", vendor_id: 1, resource_type_id: 19> 

Warum funktioniert die has_one: resource, through: :contract_resource Assoziation hier nicht?

Antwort

Verwandte Themen