RahulOnRails
RahulOnRails

Reputation: 6542

Unable to access value from database in Rails

I am facing such strange problem. Please have a look over the code

today_date = Time.now.strftime('%Y-%m-%d')

patient_id = session[:patient_id]

query = QueryContainedModel.find(:first, :conditions => ["query_type = ?", 'appointment_reminder'])

This above query generates query_value in below form ie query_value

query_value = "SELECT PATIENT.NAME AS 'first_name' FROM PATIENT, REMINDER WHERE PATIENT.ID = '#{session[:patient_id]}' 
AND REMINDER.PATIENT_ID = '#{session[:patient_id]}' AND REMINDER.DATE >= '#{today_date}'"

But I am unable to fetch values in below code

@value = ModelName.find_by_sql "#{query_value}"
@value.each do |value|
  puts value.
end

Upvotes: 1

Views: 77

Answers (2)

sameera207
sameera207

Reputation: 16629

You problem is you are trying to access a session value inside a model, But sessions are not visible to models, but only to views, helpers and controllers.

Try passing patient_id as a parameter

For example, in your controller

query = QueryContainedModel.find(:first, :conditions => ["query_type = ? and patient_id = ?", 'appointment_reminder', session[:patient_id]]

Upvotes: 0

Michael De Silva
Michael De Silva

Reputation: 3818

Supposing query = QueryContainedModel.find(:first, :conditions => ["query_type = ?", 'appointment_reminder']) works, why are you switching to SQL in the next bit of code? You can easily continue to build off query itself - I'm assuming your query works in this case.

Upvotes: 1

Related Questions