(MOVED FROM RUBY FORUM) How can I create an if statement to check if a variable contains a value? If it does contain a value, I want to send an email. I''m doing the following but it doesn''t work... -------------------------------------------------------- EXTRACT -------------------------------------------------------- def create @appointment = Appointment.new(params[:appointment]) respond_to do |format| if @appointment.save # send email only if a client email is set if defined?(appointment.client.email) Notifier.appointment_booked(@appointment).deliver end -------------------------------------------------------- Alex Stahl wrote in post #949576:> Looks like you''re checking whether or not the variable itself exists, as > opposed to if it contains a value. > > Try this instead: > > if appointment.client.email > #do stuff & things > end > > In this instance, if ''email == nil'', the statement will evaluate to > false.appointment belongs to client. so the appointments table has a client_id column. and the clients table has an email column. I think the variable its supposed to be set and empty. I tried your suggestion... def create @appointment = Appointment.new(params[:appointment]) respond_to do |format| if @appointment.save # send email only if a client email is set if @appointment.client.email Notifier.appointment_booked(@appointment).deliver end But I see the logs and it seems that it tries to send an email even if there is no email for the client. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You can try this Notifier.appointment_booked(@appointment).deliver unless @appointment.client.email.nil? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Luis Saffie wrote in post #949585:> You can try this > > > Notifier.appointment_booked(@appointment).deliver unless > @appointment.client.email.nil?According to the logs, it''s still trying to send an email. Note "Sent mail to" but of course, it''s empty because there is no email for that client. --------------- LOG TRACE --------------- Rendered notifier/appointment_booked.html.erb (5.9ms) Rendered notifier/appointment_booked.text.erb (0.3ms) Sent mail to (75ms) Date: Tue, 12 Oct 2010 13:08:51 -0500 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
right, I was checking for nil but that''s a string "". Instead do this. Notifier.appointment_booked(@appointment).deliver unless @appointment.client.email.empty? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Luis Saffie wrote in post #949592:> right, I was checking for nil but that''s a string "". Instead do this. > > Notifier.appointment_booked(@appointment).deliver unless > @appointment.client.email.empty?AWESOME!!! Finally it works! Thank you so much :D -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 12 October 2010 19:18, Luis Saffie <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> right, I was checking for nil but that''s a string "". Instead do this. > > Notifier.appointment_booked(@appointment).deliver unless > @appointment.client.email.empty?Better off using the Rails method .blank? which returns true for nil, empty strings, empty arrays, all sorts: @appointment.client.email.blank? http://api.rubyonrails.org/classes/Object.html#method-i-blank -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling wrote in post #949603:> On 12 October 2010 19:18, Luis Saffie <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> right, I was checking for nil but that''s a string "". Instead do this. >> >> Notifier.appointment_booked(@appointment).deliver unless >> @appointment.client.email.empty? > > Better off using the Rails method .blank? which returns true for nil, > empty strings, empty arrays, all sorts: > > @appointment.client.email.blank? > > http://api.rubyonrails.org/classes/Object.html#method-i-blankTrue! Worked too :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, you can do the following: unless appointment.client.email.blank? # do something end Next, you may want to do additional because some objects in the chain could be nil. Good luck, -Conrad Sent from my iPhone On Oct 12, 2010, at 10:53 AM, "Leonel *.*" <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> (MOVED FROM RUBY FORUM) > > How can I create an if statement to check if a variable contains a > value? If it does contain a value, I want to send an email. I''m doing > the following but it doesn''t work... > > -------------------------------------------------------- > EXTRACT > -------------------------------------------------------- > def create > @appointment = Appointment.new(params[:appointment]) > > respond_to do |format| > if @appointment.save > > # send email only if a client email is set > if defined?(appointment.client.email) > Notifier.appointment_booked(@appointment).deliver > end > -------------------------------------------------------- > > > Alex Stahl wrote in post #949576: >> Looks like you''re checking whether or not the variable itself exists, as >> opposed to if it contains a value. >> >> Try this instead: >> >> if appointment.client.email >> #do stuff & things >> end >> >> In this instance, if ''email == nil'', the statement will evaluate to >> false. > > appointment belongs to client. so the appointments table has a client_id > column. and the clients table has an email column. > > I think the variable its supposed to be set and empty. I tried your > suggestion... > def create > @appointment = Appointment.new(params[:appointment]) > > respond_to do |format| > if @appointment.save > > # send email only if a client email is set > if @appointment.client.email > Notifier.appointment_booked(@appointment).deliver > end > > But I see the logs and it seems that it tries to send an email even if > there is no email for the client. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 12, 2010, at 12:19 PM, Conrad Taylor wrote:> Hi, you can do the following: > > unless appointment.client.email.blank? > # do something > end > > Next, you may want to do additional because some objects in the chain could be nil.try() comes in handy for this... unless appointment.client.try(:email).blank? -philip> Good luck, > > -Conrad > > Sent from my iPhone > > On Oct 12, 2010, at 10:53 AM, "Leonel *.*" <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> (MOVED FROM RUBY FORUM) >> >> How can I create an if statement to check if a variable contains a >> value? If it does contain a value, I want to send an email. I''m doing >> the following but it doesn''t work... >> >> -------------------------------------------------------- >> EXTRACT >> -------------------------------------------------------- >> def create >> @appointment = Appointment.new(params[:appointment]) >> >> respond_to do |format| >> if @appointment.save >> >> # send email only if a client email is set >> if defined?(appointment.client.email) >> Notifier.appointment_booked(@appointment).deliver >> end >> -------------------------------------------------------- >> >> >> Alex Stahl wrote in post #949576: >>> Looks like you''re checking whether or not the variable itself exists, as >>> opposed to if it contains a value. >>> >>> Try this instead: >>> >>> if appointment.client.email >>> #do stuff & things >>> end >>> >>> In this instance, if ''email == nil'', the statement will evaluate to >>> false. >> >> appointment belongs to client. so the appointments table has a client_id >> column. and the clients table has an email column. >> >> I think the variable its supposed to be set and empty. I tried your >> suggestion... >> def create >> @appointment = Appointment.new(params[:appointment]) >> >> respond_to do |format| >> if @appointment.save >> >> # send email only if a client email is set >> if @appointment.client.email >> Notifier.appointment_booked(@appointment).deliver >> end >> >> But I see the logs and it seems that it tries to send an email even if >> there is no email for the client. >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.