2

我有一个连接到 AS400 DB 的现有模型,我需要创建一个类似的新模型,它连接到一个新文件并提取一个字段(一个电子邮件地址),然后循环 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

1

鲍勃。你离这里大约10英里!

“连接到新文件”是指新表吗?set_table_name "DISTJ01" 似乎设置了表格(通常表格会自动与模型名称相同)。

模型中现有的两种方法似乎无关紧要。

似乎您的新方法应该只访问 emal23 并执行逻辑。(但我根本不是模特大师。)

或者你的意思是它连接到 Distj01 表并写入新的文件/表 weboel23?

好的,我不是现有的数据库专家,但我认为您可以在命令行创建模型

rails generate model

或者,如果您不介意添加更多文件,您可以生成一个完整的 mvc

rails generate scaffold

在任何一种情况下,您都会给它一个表名,然后是字段和数据类型

rails generate model Weboel23 emal23:string

如果可行,则可以运行 rails 控制台

rails console

并查看应用程序以查看其中的内容。

irb(main):001:0> Weboel23.all

或者

> Weboel23.find(1)

查找 id 为 1 的记录

关于 rails 命令行的信息在这里: http: //guides.rubyonrails.org/command_line.html#rails-generate

于 2013-04-25T20:24:51.250 回答