0

I have a has many through association that I have included below. I can retrieve the record, but cannot access any of the attributes by a direct call. Below are the results of

  • Lead.first.case_details
  • Lead.first.case_details.case_number

I think i should note that all my attr_accessible are :as => admin. If that is causing the problem then i am just looking for a secure way to get and set the attributes so that the users cannot

Lead.first.case_details

Lead Load (0.0ms)  SELECT "leads".* FROM "leads" LIMIT 1
CaseDetail Load (0.0ms)  SELECT "case_details".* FROM "case_details" INNER JOIN "case_detail_leads" ON "case_details"."id" = "case_detail_leads"."case_detail_id" WHERE "case_detail_leads"."lead_id" = 1
[#<CaseDetail id: 1, case_number: "131", style_of_case: "V", case_type: "CC", judge: "DIAL", date_filed: "2013-03-17 05:00:00", first_charge_id: nil, first_lead_id: 1, location_id: nil, legalscraper_id: nil, created_at: "2013-03-30">]

Lead.first.case_details.case_number

Lead Load (1.0ms)  SELECT "leads".* FROM "leads" LIMIT 1
#<Class:0x5d351d8>: undefined method `case_number' for #<ActiveRecord::Relation:0x5d360d8>
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/relation/delegation.rb:45:in `method_missing'
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/collection_proxy.rb:100:in `method_missing'
from (irb):69
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from E:/Sites/aws/script/rails:6:in `require'
from E:/Sites/aws/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

Models

class Lead < ActiveRecord::Base
  has_many :case_detail_leads
  has_many :case_details, :through => :case_detail_leads

class LeadOffense < ActiveRecord::Base
  belongs_to :lead
  belongs_to :offense

class Offense < ActiveRecord::Base
  has_many :lead_offenses
  has_many :leads, :through => :lead_offenses
4

1 回答 1

0

Lead.first.case_details results as an Array which doesn't have a methode case_number. What you can do:

  • Lead.first.case_details.first.case_number
  • Lead.first.case_details.map{|case_detail| case_detail.case_number}
于 2013-03-30T18:59:42.490 回答