2016-11-18 92 views
0

工作,我有一個相當複雜的模型建立:軌道4「鏈接」 HAS_ONE:通過關係與堅持,但沒有新的對象

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 

注意上面的兩個類都沒有命名空間,而下方的關聯模型所有人都住在Billing命名空間中。

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 

我的問題是與VendorLineItem類,在這裏我需要獲取相關resource

如果我正在處理持久數據,一切都很好。在Rails控制檯:

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> 

如果OTOH,我有一個新的實例的工作中,resource回來爲無(軌道甚至沒有嘗試;沒有SQL發出)。

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 

但我可以通過協會的明確鏈訪問資源:

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> 

我還可以找到通過has_one :contract_resource, through: :unit_rate協會的資源沒有問題:

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> 

爲什麼has_one: resource, through: :contract_resource協會不在這裏工作?

回答