Rails拆分代码不起作用

时间:2013-05-29 05:19:49

标签: ruby-on-rails ruby-on-rails-2

我正在使用rails 2.3。在我的应用程序中,它使用

val =  Party.find(:all, :conditions => [" type in ('Physician') || id in (?)",PartyLabel.find(:all,:conditions=>"label_id=#{Label.find_by_label("Can Schedule").id}").collect{|p| p.party_id if Party.find(p.party_id).respond_to?("provider_organizations")}], :with_disabled => true).select{|physician| not physician.provider_organizations.blank? }.collect{|enum| [enum.display_name_schedule, enum.id]}

代码实现一些要求。现在我想将代码分成两部分。

1. phys = Physician.find(:all, :include => :provider_organizations, :with_disabled => true).select{|physician| not physician.provider_organizations.blank? }.collect{|enum| [enum.display_name_schedule, enum.id]}

它正常工作..第二部分将是

2. sch = Party.find(:all, :include => [:as_labels], :conditions => {:label => {:label => "Can Schedule"}}.respond_to?("provider_organizations")).select{|physician| not physician.provider_organizations.blank? }.collect{|enum| [enum.display_name_schedule, enum.id]}

它显示NoMethodError (undefined method 'provider_organizations' for #<ProviderOrganization:0x1ab81c20>):错误消息...任何评论都可以欣赏..

1 个答案:

答案 0 :(得分:0)

看起来respond_to?("provider_organizations")被称为错误的对象。这是你的代码#2:

sch = Party.find(
  :all,
  :include => [:as_labels],
  :conditions => {
    :label => {
      :label => "Can Schedule"
    }
  }.respond_to?("provider_organizations")  # What's this ???
).select{ |physician|
  not physician.provider_organizations.blank?
}.collect{ |enum|
  [enum.display_name_schedule, enum.id]
}

如果我理解正确,respond_to?应该在select

...
).select{ |physician|
  physician.respond_to?("provider_organizations") && not physician.provider_organizations.blank?
}.collect{ ...