使用具有许多通过rails查询无法访问模型属性

时间:2013-03-30 16:58:43

标签: mysql ruby-on-rails-3 activerecord

我有很多通过协会,我在下面包括。我可以检索记录,但无法通过直接调用访问任何属性。以下是

的结果
  • Lead.first.case_details
  • Lead.first.case_details.case_number

我想我应该注意到我所有的attr_accessible都是:as =>管理员。如果这是导致问题,那么我只是寻找一种安全的方式来获取和设置属性,以便用户不能

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

模型

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

1 个答案:

答案 0 :(得分:0)

Lead.first.case_details结果为没有方法case_number的数组。你能做什么:

  • Lead.first.case_details.first.case_number
  • Lead.first.case_details.map{|case_detail| case_detail.case_number}