searching a model in rails for 2 values?
I wrote this retrieval statement to check if an appointment being saved or created dosent conflict with one thats already saved. but its not working, can someone please point me to where I'm go开发者_StackOverflowing wrong?
@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}
)
the error is from the :date_of_appointment => @new_appointment.date_of_appointment
this will always be false as:
thank you
At face value, there doesn't appear to be anything wrong with your syntax. My guess is that @new_appointment
isn't containing the values you're expecting, and thus the database query is returning different values than you expect.
Try dumping out @new_appointment.inspect
or check the logfiles to see what SQL the finder is producing, or use
Appointment.send(:construct_finder_sql, :conditions => {
:date_of_appointment => @new_appointment.date_of_appointment,
:trainer_id => @new_appointment.trainer_id
})
to see the SQL that will be generated (construct_finder_sql
is a protected ActiveRecord::Base
method).
Update based on your edit
@new_appointment = :appointment
should be something like @new_appointment = Appointment.new(params[:appointment])
. :appointment
is just a symbol, it is not automatically related to your params unless you tell it to.
精彩评论