Array and Hash Iteration in Ruby - Rails -
the params' hash keys mix of camelcase, lowercase , uppercase characters:
params = {"refreshtime"=>"0", "lang"=>"tr", "amount"=>"50", "acqbin"=>"490740"}
the array, list of 1 of models' column names, lowercase same values keys of params hash =>
columns = ["refreshtime", "lang", "amount", "acqbin", ......]
i'm trying match hash keys members of array create new record in controller =>
def create_transaction @transaction = ordertransaction.new( params.each |k, v| columns.each |i| if == k.downcase "#{i}: params[:#{k}]," end end end ) end
but piece of code isn't working expected. seems i'm doing wrong in line of;
#{i}: #{v}
what missing here?
by way, old way job, causes many lines of code =>
@transaction = ordertransaction.new( refreshtime: params[:refreshtime], lang: params[:lang], amount: params[:amount], acqbin: params[:acqbin], ... )
you this
@transaction = ordertransaction.new(hash[params.map{|k,v|[k.downcase,v]}])
this creates new hash lowercase keys , uses initialize ordertransaction.
to include keys appear in columns array
@transaction = ordertransaction.new(hash[params.map{|k,v|[k.downcase,v]}.select{|k,v|columns.include?(k)}])
Comments
Post a Comment