Active Record

Apart from Technical Topics, increasingly i find myself wanting to write about other topics that interests me - Entreprenuership and Business, Leadership, Africa, Service Industry (e.g Hotels, Restaurant). My writings would always represent my opinions and it might change from time to time.

Implement 'Select column_name from table_name where condition' in Active Record

I needed an array from the data contained in a particular database column based on a condition, so i get the array of active record rows: array_of_rows = TableName.find(:all, :conditions => {:column_name => [‘col_data1’,‘col_data2’]}) Then use the array map! function to replace the active record objects with the column_name value array_of_rows.map!{|item| item.column_name} Doing some search got me: array_of_rows = TableName .find(:all,:select=>‘column_name’ :conditions => {:column_name => [‘col_data1’,‘col_data2’]}).map(&:column_name) And i like this better, concise ….

Active Record find by Column Name

In recent days been doing a lot of ruby, which means i tend to use Active Record as well. I had written some scripts where i was selecting records that matched a criteria such as: @table1.table2s.select{|e| e.column_name == 1234} but as i need to sort my result and also give some more conditions to filter the data, i need alternatives to this above query and i ended up with the two lines of code below: