开发者

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


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.


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]]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜