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

3

has_many关系应该总是复数:insurance_providers

如果你这样做client.insurance_providers,它将返回一个数组。

所以这应该工作:Client.joins(:insurance_providers).first.insurance_providers.first.copay

于 2013-10-23T15:23:28.687 回答
2

Client.joins(:insurance_provider).first.insurance_provider是一个 InsuranceProvider 对象数组,因此您已从数组中选择对象并发送共付额消息。

另外仅供参考,当我们根据rails使用has_many关联时,它应该是复数,也就是说你的模型应该是这样的,

class Client < ActiveRecord::Base
  has_many :insurance_providers
end
于 2013-10-23T15:34:22.440 回答
1

原因是因为您的关系被定义为一对多,并且insurance_provider在多方面。简而言之,你得到的不是单个对象,而是一个集合。注意在堆栈跟踪中,它显示“...ActiveRecord::Associations::CollectionProxy... 的未识别方法 'copay'”。这是那里的一个指标。因此,您需要做的是使用.each、 或调用.first.last其他方法遍历集合以获取您需要的值。基本上,集合代理的行为就像一个数组,所以你可以把它当作一个数组来对待。

于 2013-10-23T15:28:06.087 回答