ROR conditional if statement not executing - ruby-on-rails

I have this section in model:
Updated 07-25
if sign_on.acctypw1.strip == "DS" or sign_on.acctypw1.strip == "DSD"
rec = SignOn.find_by_userw1!(login)
webid = rec.prfdstw1
end
weboel23 = Weboel23.first(:conditions => {:act223 => webid}, :select => "emal23")
approval0=Weboel23.find_by_account0(!webid)
approval1=Weboel23.find_by_account1(!webid)
approval2=Weboel23.find_by_account2(!webid)
approval3=Weboel23.find_by_account3(!webid)
approval4=Weboel23.find_by_account4(!webid)
this code does appear to be working however when:
sign_on.acctypw1.strip == "DSD"
this code doesn't seem to run:
webid = rec.prfdstw1 weboel23 = Weboel23.first(:conditions => {:act223 => webid}, :select => "emal23") approval0=Weboel23.get_email_by_account0(webid)
approval1=Weboel23.get_email_by_account1(webid) approval2=Weboel23.get_email_by_account2(webid)
approval3=Weboel23.get_email_by_account3(webid) approval4=Weboel23.get_email_by_account4(webid)
when it is:
sign_on.acctypw1.strip == "DS"
it will run and return proper result. I need both cases to execute. Any ideas??

Related

How to exclude record in elastic Search in ASP.Net MVC

I have to use the elastic search for the show the document.
I have written a query like below.
var result = elastic.Search<DocPosts>(s => s.Index(indexname).AllTypes().From(data.from).Size(data.PageSize).Query(q => q.Bool(b => b.Should(sh =>
sh.Match(mt1 => mt1.OnField(f => f.Text).Query(SQuery)) ||
sh.Match(mt2 => mt2.OnField(f => f.Title).Query(SQuery))
))).Highlight(h => h
.OnFields(f => f.OnField(d => d.DocText).PreTags("<mark>").PostTags("</mark>"), f => f.OnField(d => d.Title).PreTags("<mark>").PostTags("</mark>"))
).Filter(f => f.Term(t => t.Id, Id)));
this is working perfectly.
in this, I want to exclude some record that has come in below query.
var Proc = (from p in db.Pro where p.ParId == ParId && (u.UserId != temp.UserId || u.UserId == null)
select p.procID).ToList();
how can I perform this filter or exclude from elastic search?

Rails/MongoDb Search and Refine Search Implementation

I have a search functionality in my app which works as the following
Homepage: user selects location from a drop-down and then enters a search key-word and searches to get a set of results
Search Page (REfine Search Options): Once the user hits this page from the above he will be provided with more options to refine the search and narrow the results.
Right now we are implementing as follows but i am assuming as the paramenters increase over 5-7 the number of combinations will increase the number of if-else-elseif statement as well.
#refine search
#search = params[:search]
if params[:city].present? && params[:location_ids].present? && params[:search].blank?
#blood_banks = BloodBank.where(
{ :city_id => "#{#city}" }).where(
{ :location_id.in => params[:location_ids] })
elsif params[:search].present? && params[:location_ids].blank?
#blood_banks = BloodBank.where(
{ :bb_name => /##search/i })
elsif params[:search].present? && params[:city].present? && params[:location_ids].present?
#blood_banks = BloodBank.where(
{ :city_id => "#{#city}" }).where(
{ :location_id.in => params[:location_ids] }).where(
{ :bb_name => /##search/i })
end
Which is the best way to implement the same.
How do you achieve below code,
if params[:gender].present?
if params[:gender] == "male"
#doctors = Doctor.where( :gender => "Male")
end
if params[:gender] == "female"
#doctors = Doctor.where( :gender => "Female")
end
if params[:gender] == "any"
#doctors = Doctor.where( :gender => "Male") || Doctor.where( :gender => "Female")
end
end
Mongoid's where returns a Mongoid::Criteria and Mongoid::Criteria responds to where by returning another Mongoid::Criteria. This means that you can build your query piece by piece:
#blood_banks = BloodBank.all
if params[:city].present?
#blood_banks = #blood_banks.where(:city_id => params[:city])
end
if params[:location_ids].present?
#blood_banks = #blood_banks.where(:location_id.in => params[:location_ids])
end
...
As far as the second part goes, if you're searching for any gender then just leave it out entirely, then you can do things like:
#doctors = Doctor.all
genders = { 'male' => 'Male', 'female' => 'Female' }
if genders.has_key? params[:gender]
#doctors = #doctors.where(:gender => genders[params[:gender]]
end
Searching for any gender is the same not filtering on gender at all so the nil and 'all' cases are the same. Then you can handle the input and :gender values with a simple lookup table.

Rails CSV Import Duplicates Created

I'm loading a lot of data into my application. I have over 40 models with data that I'm loading into it. All but one of these tables are importing fine. In the one table I'm having problems with every row is duplicated, so rather than having 234,225 rows I end up with 468,450 rows. Definitely not what I'm looking for. Each row is perfectly duplicated.
As part of this import I'm also flattening out the data structure. So I'm populating lookup fields into the main sku table.
Here is my code I'm using to import this skus table.
namespace :upload_data do
desc "Load Skus"
task :load_skus => :environment do
require 'csv'
require 'net/ftp'
start_time = Time.now
ftp = Net::FTP.new('<ftp location>')
ftp.login(user = "<username>", passwd = "<password>")
ftp.passive = true
ftp.get("sku.csv", File.basename( "/data/sku.csv"))
ftp.quit()
upload = File.join('sku.csv')
logcount=0
CSV.foreach(upload, :headers => false) do |row|
# roa = Roa.find_by_roa_code(row[21])
# if roa
# roa_name = roa.roa_name
# else
# roa_name = nil
# end
#ROA
roa = Roa.find_by_roa_code(row[21])
roa_name = roa && roa.roa_name
#THER
ther = Ther.find_by_thera_class_code_int(row[50] && row[50].to_i)
thera_class_desc = ther && ther.thera_class_desc
#DEA
dea = Dea.find_by_dea_class_code(row[45])
dea_class_desc = dea && dea.dea_class_desc
#LBLY2K
lbly2k = Lbly2k.find_by_mfg_code(row[56])
labeler_code = lbly2k && lbly2k.labeler_code
rebate_status = lbly2k && lbly2k.rebate_status
rebate_effective_date = lbly2k && lbly2k.rebate_effective_date
rebate_termination_date = lbly2k && lbly2k.rebate_termination_date
#FORM
form = Form.find_by_master_form_code(row[22])
form_desc = form && form.form_desc
#PRODCAT
prodcat = Prodcat.find_by_product_category(row[26] && row[26].to_i.to_s)
product_cat_desc = prodcat && prodcat.product_cat_desc
#EXPGENRC
expgenrc = Expgenrc.find_by_gcr_code(row[52])
gcr_exp_name = expgenrc && expgenrc.gcr_exp_name
#GCR
gcr = Gcr.find_by_gcr_code(row[52])
gcr_name = gcr && gcr.gcr_name
#MFORM
mform = Mform.find_by_master_form_code(row[22])
master_form_desc = mform && mform.master_form_desc
#Orange Book Data
orange = Orange.find_by_orange_book_code(row[31])
orange_book_desc = orange && orange.orange_book_desc
#EXDRUG
exdrug = Exdrug.find_by_exceptional_drug(row[44])
exceptional_drug_desc = exdrug && exdrug.exceptional_drug_desc
#MEASURE
measure = Measure.find_by_measure_code(row[39])
measure_desc = measure && measure.measure_desc
#JCODE
jcode = Jcode.find_by_ndc(row[13] && row[13].gsub('-', ''))
puts "found jcode: #{jcode.inspect}"
j_code = jcode && jcode.j_code
j_code_desc = jcode && jcode.j_code_desc
#GFC
gfc = Gfc.find_by_gcr_code(row[52])
gfc_master_code = gfc && gfc.gfc_master_code
gfc_deact = gfc && gfc.gfc_deact
#packstren
packstren = Packstren.find_by_ndc(row[13] && row[13].gsub('-', ''))
puts "Found Packstren #{packstren.inspect}"
package_ID = packstren && packstren.package_ID
puts "Package_ID is #{package_ID}"
strength_code = packstren && packstren.strength_code
#STRENGTH
strength = Strength.find_by_strength_code(strength_code)
puts "Found strength: #{strength.inspect}"
strength_name_long = strength && strength.strength_name_long
puts "Strength name long is #{strength_name_long}"
#EXPSTREN
expstren = Expstren.find_by_strength_code(strength_code)
strength_exp_name = expstren && expstren.strength_exp_name
puts "Strength_exp_name is #{strength_exp_name}"
#AWP
awp = Awp.find_by_ndc(row[13] && row[13].gsub('-', ''))
puts "Found awp: #{awp.inspect}"
effective_date = awp && awp.effective_date
puts "found effective_date: #{effective_date}"
manufacturer_cmsid = awp && awp.manufacturer_cmsid
puts "found manufacturer_cmsid: #{manufacturer_cmsid}"
package_cmsid = awp && awp.package_cmsid
puts "found package_cmsid: #{package_cmsid}"
Sku.create(:record_change => row[0],
:record_change_date => row[1],
:price_change_date => row[2],
:add_flag => row[3],
:add_date => row[4],
:deactivate_flag => row[5],
:deactivate_date => row[6],
:reactivate_flag => row[7],
:reactivate_date => row[8],
:filler1 => row[9],
:prev_ndc_awp_eff_date => row[10],
:prev_ndc_awp_pack_price => row[11],
:prev_ndc_disc_date => row[12],
:ndc => row[13].gsub('-',''),
:j_code => j_code,
:j_code_desc => j_code_desc,
:package_ID => package_ID,
:strength_code => strength_code,
:filler2 => row[14],
:ndc_orig_config => row[15].gsub('-',''),
:ndc10 => row[16].gsub('-',''),
:ndc_change_flag => row[17],
:prev_ndc => row[18],
:prev_ndc_config => row[19],
:prev_ndc10 => row[20],
:roa_code => row[21],
:roa_name => roa_name,
# :roa_name => Roa.find_by_roa_code(row[21]) && Roa.find_by_roa_code(row[21]).roa_name,
:master_form_code => row[22],
:master_form_desc => master_form_desc,
:form_desc => form_desc,
:product_name => row[23],
:additional_desc => row[24],
:mfg_name_long => row[25],
:product_category => row[26],
:product_cat_desc => product_cat_desc,
:solid_liquid => row[27],
:form_code => row[28],
:metric_size => row[29],
:strength_name => row[30],
:orange_book_code => row[31],
:orange_book_desc => orange_book_desc,
:filler3 => row[32],
:orange_book_std_flag => row[33],
:unit_dose_flag => row[34],
:dispensing_unit_flag => row[35],
:package_size => row[36],
:package_qty_code => row[37],
:product_size => row[38],
:measure_code => row[39],
:measure_desc => measure_desc,
:filler4 => row[40],
:top_volume_rank => row[41],
:single_source_flag => row[42],
:maintenance_drug_flag => row[43],
:exceptional_drug => row[44],
:exceptional_drug_desc => exceptional_drug_desc,
:dea_class_code => row[45],
:dea_class_desc => dea_class_desc,
:filler5 => row[46],
:filler6 => row[47],
:desi_drug_flag => row[48],
:desi_effective_date => row[49],
:thera_class_code => row[50],
:thera_class_desc => thera_class_desc,
:generic_class_code => row[51],
:gcr_code => row[52],
:gcr_exp_name => gcr_exp_name,
:gcr_name => gcr_name,
:gfc_master_code => gfc_master_code,
:gfc_deact => gfc_deact,
:gfc_code => row[53],
:stc_package_size => row[54],
:unit_of_measure => row[55],
:mfg_code => row[56],
:labeler_code => labeler_code,
:rebate_status => rebate_status,
:rebate_effective_date => rebate_effective_date,
:rebate_termination_date => rebate_termination_date,
:wac_start_flag => row[57],
:wac_p_price => row[58],
:wac_u_price => row[59],
:wac_eff_date => row[60],
:wac1pre_p_price => row[61],
:wac1pre_u_price => row[62],
:wac1pre_eff_date => row[63],
:wac2pre_p_price => row[64],
:wac2pre_u_price => row[65],
:wac2pre_eff_date => row[66],
:filler7 => row[67],
:filler8 => row[68],
:filler9 => row[69],
:awp_start_flag => row[70],
:awp_p_price => row[71],
:awp_u_price => row[72],
:awp_eff_date => row[73],
:awp1pre_p_price => row[74],
:awp1pre_u_price => row[75],
:awp1pre_eff_date => row[76],
:awp2pre_p_price => row[77],
:awp2pre_u_price => row[78],
:awp2pre_eff_date => row[79],
:dp_start_flag => row[80],
:dp_p_price => row[81],
:dp_u_price => row[82],
:dp_eff_date => row[83],
:dp1pre_p_price => row[84],
:dp1pre_u_price => row[85],
:dp1pre_eff_date => row[86],
:dp2pre_p_price => row[87],
:dp2pre_u_price => row[88],
:dp2pre_eff_date => row[89],
:ful_start_flag => row[90],
:ful_p_price => row[91],
:ful_u_price => row[92],
:ful_eff_date => row[93],
:ful_pre_p_price => row[94],
:ful_pre_u_price => row[95],
:ful_pre_eff_date => row[96],
:ful2_pre_p_price => row[97],
:ful2_pre_u_price => row[98],
:ful2_pre_eff_date => row[99],
:srp_start_flag => row[100],
:srp_p_price => row[102],
:srp_u_price => row[102],
:srp_eff_date => row[103],
:srp1pre_p_price => row[104],
:srp1pre_u_price => row[105],
:srp1pre_eff_date => row[106],
:srp2pre_p_price => row[107],
:srp2pre_u_price => row[108],
:srp2pre_eff_date => row[109])
logcount += 1
end
total_time = Time.now - start_time
puts "Successfully added #{logcount} Skus. Elapsed time #{total_time} seconds."
end
end
Any ideas what I'm doing which is causing each row to be imported?
Thanks

How to return hash values as a string using to_s

This is my code:
def return_rider_values(pol_option, pro_endorsement, prop_coverage, *par)
rider_values
par.each do |p|
rider_values << RiderValue.find_all_by_rider_id(p)
end
rider_hash = { }
rider_values.each do |rv|
if rv.attributes["name"].downcase == "yes"
rider_hash.merge!({par[0].to_s => rv.attributes['id'].to_s})
elsif rv.attributes["position"] == pol_option.to_i && rv.attributes["rider_id"] == par[1]
rider_hash.merge!({par[1].to_s => rv.attributes["id"].to_s})
elsif rv.attributes["position"] == prop_coverage.to_i && rv.attributes["rider_id"] == par[2]
rider_hash.merge!({par[2].to_s => rv.attributes["id"].to_s})
elsif rv.attributes["position"] == pro_endorsement.to_i && rv.attributes["rider_id"] == par[3]
rider_hash.merge!({par[3].to_s => rv.attributes["id"].to_s})
end
end
rider_hash
end
The output looks like this:
rider_hash #=> '22' -> 58
'23' -> 61
'25' -> 66
'26' -> 68
I was expecting, and need apparently since it's not working later down the line:
rider_hash #=> '22' -> '58'
'23' -> '61'
'25' -> '66'
'26' -> '68'
I don't know why the lookup function later in the program wants the ids to be strings instead of ints. I just know that it does, and I can't change it since lots of other methods use it.
I have to_s on both the hash key and value. I realize that in Ruby 1.9 to_s is an alias for inspect but even in the Hash documentation it says that, inspect or to_s is supposed to "Return the contents of this hash as a string."
So why is only the key being returned as a string?
You have an array of hashes so try this:
def return_rider_values
par = [1,2,3,6]
rider_hash = {}
rider_values = [element1: {:attributes => {:id => 1, :name => 'yes', :position => 1, :rider_id => 1}},
element2: {:attributes => {:id => 2, :name => 'no', :position => 2, :rider_id => 2}},
element3: {:attributes => {:id => 3, :name => 'something', :position => 1, :rider_id => 3}},
element4: {:attributes => {:id => 4, :name => 'something_else', :position => 2, :rider_id => 6}}]
rider_values.each_with_index do |hash, idx|
rider_values[idx].each_pair do |k, v|
if v[:attributes][:name].downcase == "yes"
rider_hash.merge!({par[0].to_s => v[:attributes][:id].to_s})
elsif v[:attributes][:position] == 2 && v[:attributes][:rider_id] == par[1]
rider_hash.merge!({par[1].to_s => v[:attributes][:id].to_s})
elsif v[:attributes][:position] == 3 && v[:attributes][:rider_id] == par[2]
rider_hash.merge!({par[2].to_s => v[:attributes][:id].to_s})
elsif v[:attributes][:position] == 4 && v[:attributes][:rider_id] == par[3]
rider_hash.merge!({par[3].to_s => v[:attributes][:id].to_s})
end
end
end
rider_hash
end
test = return_rider_values
puts test
output: #=> {"1"=>"1", "2"=>"2"}
I ended up getting what I wanted by adding this:
rider_hash.each{ |_,v| v.replace "'#{v}'"}
But this seems like a dirty solution somehow.

rails, syntax error, unexpected ','

I have several conditions in my search.
#events = Event.search(params[:search],
:conditions => {:group_size => 1, :days => 1})
The above code is working fine. However, if I want to replace the hash with a private method, I get syntax error
syntax error, unexpected ',', expecting tASSOC
:conditions => {group_size_condition, :days => 1},
Code is as follow
#events = Event.search(params[:search],
:conditions => {group_size_condition, :days => 1})
private
def group_size_condition
if params[:groupsize] == 'single (1)'
:group_size => 1
elsif params[:groupsize] == 'couple (2)'
:group_size => 2
elsif params[:groupsize] == 'small group(3-5)'
:group_size => 3..5
else
nil
end
end
Thanks in advance
That will be work
#events = Event.search(params[:search],
:conditions => group_size_condition.merge(:days => 1))
private
def group_size_condition
case params[:groupsize]
when 'single (1)' then {:group_size => 1}
when 'couple (2)' then {:group_size => 2}
when 'small group(3-5)' then {:group_size => 3..5}
else
{}
end
end
I think you missed :group_size key
#events = Event.search(params[:search],
:conditions => {:group_size => group_size_condition, :days => 1})
I think perhaps you want to actually pass the string, as in:
def group_size_condition
if params[:groupsize] == 'single (1)'
':group_size => 1'
elsif params[:groupsize] == 'couple (2)'
':group_size => 2'
elsif params[:groupsize] == 'small group(3-5)'
':group_size => 3..5'
else
nil
end
end

Resources