Rails splited code not working

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 02:54:43

问题


I am using rails 2.3. In my application it uses

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]}

code to achieve some requirements. Now i wants to split the code in to 2 parts.

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]}

it's working fine.. and the second part will be

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]}

it shows NoMethodError (undefined method 'provider_organizations' for #<ProviderOrganization:0x1ab81c20>): error message... Any comments could be appreciated..


回答1:


It looks like respond_to?("provider_organizations") is called for a wrong object. Here is your code #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]
}

If I understand it correctly, the respond_to? should be inside the select:

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


来源:https://stackoverflow.com/questions/16806038/rails-splited-code-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!