开发者

searching within an already retrieved mysql result

I'm trying to limit the number of times I do a mysql query, as this could end up being 2k+ queries just to accomplish a fairly small result.

I'm going through a CSV file, and I need to check that the format of the content in the csv matches the format the db expects, and sometimes I try to accomplish some basic clean-up (for example, I have one field that is a string, but is sometimes in the csv as j开发者_如何学编程b2003-343, and I need to strip out the -343).

The first thing I do is get from the database the list of fields by name that I need to retrieve from the csv, then I get the index of those columns in the csv, then I go through each line in the csv and get each of the indexed columns

get_fields = BaseField.find_by_group(:all, :conditions=>['group IN (?)',params[:group_ids]])

csv = CSV.read(csv.path)
first_line=csv.first
first_line.split(',')
csv.each_with_index do |row|
    if row==0
     col_indexes=[]
     csv_data=[]
     get_fields.each do |col|
        col_indexes << row.index(col.name)
     end
    else
      csv_row=[]
      col_indexes.each do |col|
            #possibly check the value here against another mysql query but that's ugly
        csv_row << row[col]
      end
      csv_data << csv_row
    end

end

The problem is that when I'm adding the content of the csv_data for output, I no longer have any connection to the original get_fields query. Therefore, I can't seem to say 'does this match the type of data expected from the db'.

I could work my way back through the same process that got me down to that level, and make another query like this

get_cleanup = BaseField.find_by_csv_col_name(first_line[col])
  if get_cleanup.format==row[col].is_a
     csv_row << row[col]
  else 
     # do some data clean-up
  end

but as I mentioned, that could mean the get_cleanup is run 2000+ times.

instead of doing this, is there a way to search within the original get_fields result for the name, and then get the associated field?

I tried searching for 'search rails object', but kept getting back results about building search, not searching within an already existing object.

I know I can do array.search, but don't see anything in the object api about search.

Note: The code above may not be perfect, because I'm not running it yet, just wrote that off the top of my head, but hopefully it gives you the idea of what I'm going for.


When you populate your col_indexes array, rather than storing a single value, you can store a hash which includes index and the datatype.

get_fields.each do |col|
  col_info = {:row_index = row.index(col.name), :name=>col.name :format=>col.format}
  col_indexes << col_info 
end

You can then access all your data in the for loop

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜