2013-10-23 37 views
0

這裏是我的client.rb:爲什麼我無法在導軌4中的模型中使用此列?

class Client < ActiveRecord::Base 
    has_many :insurance_provider 
end 

這是通過一個控制檯會話最容易解釋:

1.9.3p448 :005 > Client.joins(:insurance_provider).first.insurance_provider 

    Client Load (1.2ms) SELECT "clients".* FROM "clients" INNER JOIN "insurance_providers" ON "insurance_providers"."client_id" = "clients"."id" ORDER BY "clients"."id" ASC LIMIT 1 
    InsuranceProvider Load (0.4ms) SELECT "insurance_providers".* FROM "insurance_providers" WHERE "insurance_providers"."client_id" = $1 [["client_id", 6]] 
=> #<ActiveRecord::Associations::CollectionProxy [#<InsuranceProvider id: 2, client_id: 6, name: "Blue Cross Blue Shield", member_id: "123456789", copay: 20, effective_on:  "2013-07-08", created_at: "2013-10-23 14:40:00", updated_at: "2013-10-23 14:40:00">]> 


1.9.3p448 :006 > Client.joins(:insurance_provider).first.insurance_provider.copay 

    Client Load (1.3ms) SELECT "clients".* FROM "clients" INNER JOIN "insurance_providers" ON "insurance_providers"."client_id" = "clients"."id" ORDER BY "clients"."id" ASC LIMIT 1 
NoMethodError: InsuranceProvider Load (0.5ms) SELECT "insurance_providers".* FROM "insurance_providers" WHERE "insurance_providers"."client_id" = $1 [["client_id", 6]] 
undefined method `copay' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_InsuranceProvider:0x007fae3e8e9d80> 
    from /Users/rabdelaz/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:121:in `method_missing' 
    from /Users/rabdelaz/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:68:in `method_missing' 
    from /Users/rabdelaz/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/collection_proxy.rb:22:in `method_missing' 
    from (irb):6 
    from /Users/rabdelaz/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start' 
    from /Users/rabdelaz/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start' 
    from /Users/rabdelaz/.rvm/gems/ruby-1.9.3-p448/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>' 
    from bin/rails:4:in `require' 
    from bin/rails:4:in `<main>' 

爲什麼我不能達到copay列?

+0

如何在'Client'定義'insurance_provider'。看起來像這是沒有正確配置。 – phoet

+0

更新了問題。客戶has_many:insurance_provider – Ramy

回答

3

has_many關係應該永遠是複數:insurance_providers

如果你做一個client.insurance_providers它會返回一個數組。

所以這應該工作:Client.joins(:insurance_providers).first.insurance_providers.first.copay

+0

好的,這裏有些東西真的很棘手。我改變了我的模型,就像你說的,但現在我得到這個:'Client.joins(:insurance_provider).first.insurance_providers.copay ActiveRecord :: ConfigurationError:在客戶端找不到名爲'insurance_provider'的關聯;也許你拼錯了?' – Ramy

+0

我認爲它應該是'連接(:insurance_providers)'複數。我更新了我的答案 – phoet

1

的原因是因爲你的關係被定義爲一個一對多,與insurance_provider在許多方面。總之,你沒有回到一個單一的對象,你收回了一個集合。注意在堆棧跟蹤中,它說「未識別的方法'copay'用於... ActiveRecord :: Associations :: CollectionProxy ...」。那是一個指標。所以,你需要做的是使用.each迭代集合,或調用.first.last或其他來獲得你需要的值。基本上,集合代理的行爲就像一個數組,所以你可以像這樣對待它。

+0

請重新閱讀我的問題。我使用'.first'方法。 – Ramy

+1

@Ramy是的,但是你在'Client'模型上調用它,而不是'insurance_providers'關聯。正如我和phoet所說的那樣,由於'insurance_providers'被定義爲'has_many',你最終會得到一組對象。這是否更有意義? –

+1

我現在開始看到問題了 - 我需要一個帶有max(:effective_on)日期的單個insurance_provider。類似於'client.insurance_providers.maximum(:effective_on).copay'。如果我無法弄清楚,請注意另一個問題。謝謝您的幫助! – Ramy

2

Client.joins(:insurance_provider).first.insurance_provider是InsuranceProvider對象的數組,因此您可以從數組中選擇對象併發送附加消息。

此外僅供參考,當我們根據軌道使用的has_many關聯,它應該是多元的,那就是你的模型應該是這樣的,

class Client < ActiveRecord::Base 
    has_many :insurance_providers 
end 
相關問題