【问题标题】:Rails splited code not workingRails拆分代码不起作用
【发布时间】:2013-05-29 05:19:49
【问题描述】:

我正在使用 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>): 错误消息...任何 cmets 都可以欣赏..

【问题讨论】:

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


    【解决方案1】:

    看起来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{ ...
    

    【讨论】:

      猜你喜欢
      • 2017-02-07
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 2020-01-10
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多