views:

43

answers:

1

Hi

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 going 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

+3  A: 

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.

Daniel Vandersluis
hi i updated the question, yes you are right, for some reason it has date_of_appointment with 3 different values, "date_of_appointment(2i)"=>"3","date_of_appointment(3i)"=>"10","date_of_appointment(1i)"=>"2012"how can i make the check? thanks
Mo
I'm not really sure what you mean by 1i, 2i and 3i...
Daniel Vandersluis
They come from date_select helper and can be converted as so: http://gist.github.com/331737
mark
the method still dosent work because it can not retrieve the appointments that match the conditions specified. appointments is always empty.
Mo
problem is with trying to compare dates. rails handles this very weird:'irb(main):014:0> dn == d=> falseirb(main):015:0> dn.to_s=> "2012-07-03"irb(main):016:0> d.to_s=> "2010-07-03"irb(main):017:0> 'two dates are the same yet if you compare they are false. any way around this?
Mo
i changed it to '@new_appointment = person.appointments.build(params[:appointment])' and it works great for the update method (identifies conflicts) but dosent work for create method. very odd. this is driving me mad, i will give you the right answer but if you know why its doing that it would be great. thanks
Mo