0

ここに私の client.rb があります:

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??

4

3 に答える 3

2

Client.joins(:insurance_provider).first.insurance_providerは InsuranceProvider オブジェクトの配列であるため、配列からオブジェクトを選択して copay メッセージを送信します。

また参考までに、レールに従って has_many 関連付けを使用する場合、複数形にする必要があります。つまり、モデルは次のようになります。

class Client < ActiveRecord::Base
  has_many :insurance_providers
end
于 2013-10-23T15:34:22.440 に答える