I need to compare a column in TableA to a column in TableB and then
select 3 columns from TableA for all the records in TableA that don’t
exist in TableB.
TableA and TableB are different but do have one field in common that i
am comparing. The tables are not related in anyway.
right now I have a find_by_sql query located in a model that looks like
this:
def self.absent_charges
find_by_sql <<-QUERY
SELECT sources.charge_id_number, sources.charge_name,
sources.offense_level
FROM sources
LEFT JOIN charges ON sources.charge_id_number =
charges.charge_id_number
WHERE charges.id IS NULL
QUERY
end
I also need to dedupe what ends up in my final collection after the
find_by_sql query, but I am not sure how to go about this. Is there a
better way to do this? Any suggestions?
My view takes the 3 columns from the TableA records and presents them
for editing before they are added to TableB.
The update_charges.rhtml view:
Add missing records to charges table
<%= start_form_tag %>| Charge id number | Charge name | Classification level | <%= render :partial => 'charge', :collection => @new_charges %>
|---|
the partial _charge.rhtml:
the relevant part of the controller:
class ImportController < ApplicationController
…
def update_charges
@new_charges = Source.absent_charges
@charge_types = ChargeType.find(:all)
if request.get?
redirect_to :action => 'update_tables' if @new_charges.empty?
else
begin
params[:charge].each do |key, row|
Charge.create!(row)
end
rescue Exception => e
flash[:notice] = "Can't update charges table: " + e
redirect_to :action => 'update_tables'
else
flash[:notice] = 'Charges table was succesfully updated.'
redirect_to :action => 'update_tables'
end
end
end
…
end
it is late, i am tired, and i am having a hard time even explaining what
needs to be done. i hope someone can help me get this on track. right
now i can get it to select the correct items, but there are duplicates
which i don’t want, and if i perform the update_charges twice in a row
the find_by_sql appends the same data and presents it twice in my view.
i guess i need to initialize the absent_charges somehow? any help
greatly appreciated.