ibm midrange - Creating a ruby-on-rails model -
i have existing model connects as400 db , need create new model connects new file , pulls 1 field (an email address) , loops through 5 times. shouldn't difficult have never done in ruby , can't seem work. point me in right direction appreciated!
here existing model:
class distributor < activerecord::base establish_connection "as400_#{rails_env}" set_table_name "distj01" # todo: 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 have preferred distributor, pull 1 , make first default that. if !preferred_distributor.blank? s = distributor.find_by_sql ["select cnam05 cnam05, cusnp1 cusnp1 distj01 cusnp1 = ?", preferred_distributor] d << { :name => s[0].cnam05, :id => s[0].cusnp1 } unless s.blank? end # if have account number, can choose purchase direct in addition choosing distributor. # per ron 4/20/11, removed this. #d << { :name => "direct purchase", :id => account_number } unless account_number.blank? # other available distributors state. s = distributor.find_by_sql ["select cnam05 cnam05, cusnp1 cusnp1 distj01 statep2 = ? , cusnp1 <> ? order cnam05", state.upcase, preferred_distributor] unless s.blank? s.each |t| d << { :name => t.cnam05, :id => t.cusnp1 } end end d end def self.validate_distributor_number distributor_number, user # need make sure distributor number assigning # valid. since comes form post, pick # number if wanted. can't have that. granted # had logon legit user, but, still # try trick us. if distributor_number.strip == user.as400_fields[:account_number].strip distributor_number else # if dsd account chooses purchase preferred distributor, use customer number # sign-on table, not distributor. if user.as400_fields[:preferred_distributor] == distributor_number , user.as400_fields[:account_type].strip == "dsd" user.as400_fields[:account_number] else d = distributor.find_by_sql ["select cusnp1 cusnp1 distj01, signons " + "where distj01.statep2 = signons.bsstcdw1 , signons.userw1 = ? , distj01.cusnp1 = ?", user.login.upcase, distributor_number] d[0].cusnp1 unless d.blank? end end end end
the new connection needs table weboel23 , filed emal23. here have far need add:
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 weboel23 act223 = ?", cono23] d << { :name => s[0].act223, :id => s[0].emal23 } unless s.blank? end end end
the model seems better failing on order.rb page when call field model missing?
weboel23 = weboel23.first(:conditions => {:cusnp1 => distributor_number}, :select => "emal23")
bob. 10 miles here!
by "connects new file" mean new table? set_table_name "distj01" seem set table (normally table automatically same model name).
the 2 existing methods in model seem irrelevent.
seems new method should access emal23 , perform logic. (but i'm not @ model guru.)
or mean connects distj01 table , writes new file/table weboel23?
ed
ok, i'm not existing db guy, think can create model @ command line
rails generate model
or, if don't mind adding more files, can generate entire mvc with
rails generate scaffold
in either case, give table name, followed fields , data types
rails generate model weboel23 emal23:string
if works, can run rails console
rails console
and peer app see what's there.
irb(main):001:0> weboel23.all
or
> weboel23.find(1)
to find record id 1
info on rails command line here: http://guides.rubyonrails.org/command_line.html#rails-generate