I have this long sql query that I am making, but I need to make ActiveRecord calls on it and cant. Is their an easy way to convert sql to active record??
sql_query = "SELECT k.id AS k_key_id, k.key, c.string, s.name as state, s.color, h.created_at, h.updated_at, l.id as l_language_id, l.language_code, u.id as user_id, u.email "
sql_query += "FROM keys k, cords c, histories h, languages l, users u, states s "
sql_query += "WHERE c.key_id = k.id "
sql_query += "AND c.language_id = l.id "
sql_query += "AND c.state_id = s.id "
sql_query += "AND h.key_id = k.id "
sql_query += "AND h.action = 'Created' "
sql_query += "AND h.user_id = u.id "
if search_parameters != ""
sql_query += "AND (k.key LIKE '%#{search_parameters}%' "
sql_query += "OR c.string LIKE '%#{search_parameters}%') "
end
sql_query += "LIMIT #{limit}"
sql_query += "OFFSET #{offset}"
return ActiveRecord::Base.connection.execute(sql_query)
This returns an array of objects that look like:
{"k_key_id"=>"1",
"key"=>"device.ios",
"string"=>"this is an ios string",
"state"=>"New",
"color"=>"red",
"created_at"=>"2015-08-05 19:57:21.342388",
"updated_at"=>"2015-08-05 19:57:21.342388",
"l_language_id"=>"1",
"language_code"=>"en",
"user_id"=>"2",
"email"=>"blah@gmail.com"}
Which is exactly what I want. All this data in one spot, combined from all my tables.
Any help would be much appreciated!