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 *.*" <[email protected]> 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 [email protected].
>> To unsubscribe from this group, send email to 
>> [email protected].
>> 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 [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to