Anuj Garg
Anuj Garg

Reputation: 167

how to convert sql query in ruby and rails?

SELECT A.FirstName, A.LastName, B.PatientId, B.RoomNumber, B.AdmissionDate, B.DischargeDate, B.MeasureCategory 
FROM DimPatient A, DimPatientStay B 
WHERE A.Id = B.PatientId AND A.FirstName = 'Anuj' AND B.MeasureCategory = 'ED'

hi some updation for this i solved this prob by

MODELNAME.find_by_sql("your sql query")

Upvotes: 0

Views: 158

Answers (3)

Debadatt
Debadatt

Reputation: 6015

You can try this to find the result from sql query in Rails

query_params = Hash.new
    sql_query = "SELECT A.FirstName, A.LastName, B.PatientId, B.RoomNumber,    B.AdmissionDate, B.DischargeDate, B.MeasureCategory 
FROM DimPatient A, DimPatientStay B 
WHERE A.Id = B.PatientId AND A.FirstName = :first_name AND B.MeasureCategory = :measure_category"

query_params[:first_name] = first_name
query_params[:measure_category] = measure_category


@query_results = ActiveRecord::Base.connection.select_all(
  ActiveRecord::Base.send("sanitize_sql_array",[sql_query, query_params] )
)

Upvotes: 1

dpaluy
dpaluy

Reputation: 3715

Suppose A is one class and B is another, you should use includes as following:

A.includes(:b).where(...) # add you condition in where

I suggest to check good video tutorials of ActiveRecord here

Upvotes: 0

JamesMacLeod
JamesMacLeod

Reputation: 76

I guess you could try:

ActiveRecord::Base.connection().execute(#your_sql_here)

Upvotes: 0

Related Questions