Technical
How To Update When ARMethod Will Not Feat

Due to improper naming convention ( a case where dba used reserved word such as type for a column name ) we had to switch to a “writable” view that would encapsulate actually table. On views none of the AR stuff works properly since meta data for a view totally different then that of table.

In any case that what I’ve written in order to over come that.

app/model


def self.update(id,params={})
    begin
      sql = "Update leb_legal_entities SET " 
      params.each do |key,value|
      value.gsub!("'","''") if !numeric?(value) &&  value.include?("'")
        sql <<  (self.numeric?(value,key) ? " #{key} = #{value} ," : " #{key} = '#{value}' ," )
      end
      sql << "WHERE legalEntityId = #{id} " 
      ActiveRecord::Base.connection.execute sql.gsub(',WHERE',' WHERE ')
    rescue Exception => e
      throw "Exception: #{e}" 
    end
  end

tests

Updating every column as well as single one


 def test_update
       assert_nothing_raised{LegalEntity.update(5291,{
                              "leb_legal_entity_type_id"=>"0",
                              "reportingCcy"=>"USD",
                              "adp"=>"5365372",
                              "reportingSec"=>"0",
                              "altKeyword"=>"",
                              "nameFull"=>"Some name",
                              "domicileCountry"=>"US",
                              "bizType"=>"1",
                              "lddCode"=>"16771",
                              "cisCode"=>"CARPAUS",
                              "keyword"=>"ARIS",
                              "nameShort"=>"Aristeia Partners, L.P."}
                  )}

    assert_equal LegalEntity.find(5291).nameFull,"Some name" 
    assert_nothing_raised{LegalEntity.update(5291,{"reportingSec" => 1})}
    assert_nothing_raised{LegalEntity.update(5291,{"nameFull" => "Jorge De'Sant"})}

  end

Numeric? method

Since, when using AR update_attributes method would automatically figure out data type ( numeric or string ) with mentioned above I’ve lost it as well. The following method helps me check for data type, as well figure out whether to convert data to int when its not supposed to be even if its numeric – see tests for easier explanation.


  def self.numeric?(value,column_name = nil)
    non_convertable_string_columns = ["adp","lddCode"]
    begin
     return false if  non_convertable_string_columns.include? column_name
     return true if Kernel.Float(value)
    rescue ArgumentError, TypeError
     return false
    end
  end

Tests


def test_numeric_function
      assert(LegalEntity.numeric? 3)
      assert_equal false,LegalEntity.numeric?("asdf")
      assert_equal false,LegalEntity.numeric?(1234,"adp")
      assert_equal false,LegalEntity.numeric?(56765,"lddCode")
  end