在rails中搜索2个值的模型?

时间:2010-08-05 21:22:54

标签: ruby-on-rails

我编写了这个检索语句,以检查保存或创建的约会是否与已保存的约会冲突。但它没有用,有人可以指点我到哪里出错吗?

@new_appointment = :appointment #which is the params of appointment being sent back from submit.
@appointments = Appointment.all(:conditions => { :date_of_appointment => @new_appointment.date_of_appointment, :trainer_id => @new_appointment.trainer_id}

错误来自:date_of_appointment => @new_appointment.date_of_appointment,这将始终为false:

谢谢

1 个答案:

答案 0 :(得分:3)

从表面看,您的语法似乎没有任何问题。我的猜测是@new_appointment不包含您期望的值,因此数据库查询返回的值与您预期的值不同。

尝试转储@new_appointment.inspect或检查日志文件以查看查找程序生成的SQL,或使用

Appointment.send(:construct_finder_sql, :conditions => {
  :date_of_appointment => @new_appointment.date_of_appointment,
  :trainer_id => @new_appointment.trainer_id
})

查看将生成的SQL(construct_finder_sql是受保护的ActiveRecord::Base方法)。


根据您的修改进行更新

@new_appointment = :appointment应该类似于@new_appointment = Appointment.new(params[:appointment]):appointment只是一个符号,除非你告诉它,否则它不会自动与你的参数相关联。