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