我有一个连接到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")发布于 2013-04-26 04:24:51
鲍勃。你离这里大约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
https://stackoverflow.com/questions/16216061
复制相似问题