我有一个连接到 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")