2

AS400 DB に接続する既存のモデルがあり、新しいファイルに接続して 1 つのフィールド (電子メール アドレス) を取得し、それを 5 回ループするような新しいモデルを作成する必要があります。これは難しいことではありませんが、Ruby でこれを行ったことがなく、動作させることができないようです。私を正しい方向に向ける助けがあれば、大歓迎です!

これが既存のモデルです。

 class Distributor < ActiveRecord::Base

 establish_connection "as400_#{RAILS_ENV}"
set_table_name "DISTJ01"

# TODO: what to return if no distributor?
# Create array of hashed distributor info.
def self.get_distributors_by_state state, preferred_distributor
    d = []
    # see validate_distributor_number below
    # If they have a preferred distributor, pull that one and make it first so it will default to that.
    if !preferred_distributor.blank?
        s = Distributor.find_by_sql ["SELECT CNAM05 cnam05, CUSNP1 cusnp1 FROM DISTJ01 WHERE CUSNP1 = ?", preferred_distributor]
        d << { :name => s[0].cnam05, :id => s[0].cusnp1 }   unless s.blank?
    end

    # If they have an account number, they can choose to purchase direct in addition to choosing a distributor.
    # Per Ron 4/20/11, removed this.
    #d << { :name => "DIRECT PURCHASE", :id => account_number } unless account_number.blank?

    # Get all other available distributors for this state.
    s = Distributor.find_by_sql ["SELECT CNAM05 cnam05, CUSNP1 cusnp1 FROM DISTJ01 WHERE STATEP2 = ? AND CUSNP1 <> ? ORDER BY CNAM05", state.upcase, preferred_distributor]
    unless s.blank?
        s.each do |t|
            d << { :name => t.cnam05, :id => t.cusnp1 }
        end
    end
    d
end

def self.validate_distributor_number distributor_number, user
    # We need to make sure that the distributor number we are assigning
    # is valid. Since it comes from a form post, somebody could pick
    # another number if they wanted. We can't have that. Granted they
    # already had to logon so they are a legit user, but, they still
    # could try to trick us.
    if distributor_number.strip == user.as400_fields[:account_number].strip
        distributor_number
    else

        # If a DSD account chooses to purchase from their preferred distributor, we use the customer number from
        # the sign-on table, not from the distributor.
        if user.as400_fields[:preferred_distributor] == distributor_number and user.as400_fields[:account_type].strip == "DSD"
            user.as400_fields[:account_number]
        else
            d = Distributor.find_by_sql ["SELECT CUSNP1 cusnp1 FROM DISTJ01, SIGNONS " +
                "WHERE DISTJ01.STATEP2 = SIGNONS.BSSTCDW1 AND SIGNONS.USERW1 = ? AND DISTJ01.CUSNP1 = ?", user.login.upcase, distributor_number]
            d[0].cusnp1 unless d.blank?
        end
    end
end

 end

新しい接続はテーブル WEBOEL23 である必要があり、フィールドは EMAL23 です。これまでのところ、何を追加する必要がありますか。

 class Weboel23 < ActiveRecord::Base

establish_connection "as400_#{RAILS_ENV}"
set_table_name "WEBOEL23"

 def self.get_email_by_account email, cono23
    d = []
    if !emal23.blank?
        s = email.find_by_sql ["SELECT ACT223 act223,EMAL23 emal23 FROM WEBOEL23 WHERE ACT223 = ?", CONO23]         
        d << { :name => s[0].act223, :id => s[0].emal23 }   unless s.blank?
      end    
   end
 end 

モデルは改善されたように見えますが、モデルからフィールドを呼び出すと、order.rb ページで失敗するようになりました。

  weboel23 = Weboel23.first(:conditions => {:cusnp1 => distributor_number}, :select => "EMAL23")
4

1 に答える 1