为什么我无法在rails 4中的模型中找到此列?

时间:2013-10-23 15:16:11

标签: ruby-on-rails

这是我的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列?

3 个答案:

答案 0 :(得分:3)

has_many关系应始终为复数:insurance_providers

如果您执行client.insurance_providers,它将返回一个数组。

所以这应该有效:Client.joins(:insurance_providers).first.insurance_providers.first.copay

答案 1 :(得分:2)

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

另外FYI当我们根据rails使用has_many关联时,它应该是复数,那就是你的模型应该是这样的,

class Client < ActiveRecord::Base
  has_many :insurance_providers
end

答案 2 :(得分:1)

原因是因为你的关系被定义为一对多,而insurance_provider就在很多方面。简而言之,你没有找回一个对象,你正在收回一个集合。请注意,在堆栈跟踪中,它表示“unidentified method'copay'for ... ActiveRecord :: Associations :: CollectionProxy ...”。那是一个指标。因此,您需要做的是使用.each迭代集合,或调用.first.last或其他任何内容来获取所需的值。基本上,集合代理的行为就像一个数组,所以你可以这样对待它。