创建ruby-on-rails模型

时间:2013-04-25 13:27:57

标签: ruby-on-rails ibm-midrange rails-activerecord

我有一个连接到AS400数据库的现有模型,我需要创建一个新模型,它连接到一个新文件并拉出一个字段(一个电子邮件地址),然后循环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")

1 个答案:

答案 0 :(得分: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