Neil Cauldwell
2008-Mar-10 20:18 UTC
Using a boolean attribute with a conditional statement
I''m betting this is a schoolboy error: I have a ''protected_profile'' boolean attribute in my user model, and I want to use it in a controller action, like so; if @user.protected_profile = true some.task else some_other.task end But even if the protected_profile boolean returns false in the browser (<%= @user.protected_profile %>), the above action doesn''t seem to work. I barely remember reading something about Ruby assigning true values in most instances - is that what is causing my problems? -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Mar-10 20:38 UTC
Re: Using a boolean attribute with a conditional statement
On 10 Mar 2008, at 20:18, Neil Cauldwell wrote:> > I''m betting this is a schoolboy error: > > I have a ''protected_profile'' boolean attribute in my user model, and I > want to use it in a controller action, like so; > > if @user.protected_profile = true > some.task > else > some_other.task > end > > But even if the protected_profile boolean returns false in the browser > (<%= @user.protected_profile %>), the above action doesn''t seem to > work. > > I barely remember reading something about Ruby assigning true values > in > most instances - is that what is causing my problems?No. Your problem is that = is the assignment operator, not the comparison operator (that''s ==) You probably just want: if @user.protected_profile some.task else some_other.task end Fred> > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Neil Cauldwell
2008-Mar-10 21:18 UTC
Re: Using a boolean attribute with a conditional statement
Frederick Cheung wrote:> On 10 Mar 2008, at 20:18, Neil Cauldwell wrote: > >> end >> >> But even if the protected_profile boolean returns false in the browser >> (<%= @user.protected_profile %>), the above action doesn''t seem to >> work. >> >> I barely remember reading something about Ruby assigning true values >> in >> most instances - is that what is causing my problems? > No. Your problem is that = is the assignment operator, not the > comparison operator (that''s ==) > You probably just want: > if @user.protected_profile > some.task > else > some_other.task > end > > FredThanks Fred. I tried your suggestion, but it doesn''t work. A user with protected_profile ''false'' will still do some.task, rather than some_other.task. The same goes when using the comparison operator, @user.protected_profile == true. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Mar-11 08:06 UTC
Re: Using a boolean attribute with a conditional statement
On 10 Mar 2008, at 21:18, Neil Cauldwell wrote:> > Frederick Cheung wrote: >> On 10 Mar 2008, at 20:18, Neil Cauldwell wrote: >> >>> end >>> >>> But even if the protected_profile boolean returns false in the >>> browser >>> (<%= @user.protected_profile %>), the above action doesn''t seem to >>> work. >>> >>> I barely remember reading something about Ruby assigning true values >>> in >>> most instances - is that what is causing my problems? >> No. Your problem is that = is the assignment operator, not the >> comparison operator (that''s ==) >> You probably just want: >> if @user.protected_profile >> some.task >> else >> some_other.task >> end >> >> Fred > > Thanks Fred. I tried your suggestion, but it doesn''t work. A user with > protected_profile ''false'' will still do some.task, rather than > some_other.task. The same goes when using the comparison operator, > @user.protected_profile == true.Are you setting protecting profile to the string ''false''? The two are completely different (and you may not expect it, but the string ''false'' is in fact true. Fred> > > > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Bush
2008-Mar-11 08:39 UTC
Re: Using a boolean attribute with a conditional statement
Neil Cauldwell wrote:> Thanks Fred. I tried your suggestion, but it doesn''t work. A user with > protected_profile ''false'' will still do some.task, rather than > some_other.task. The same goes when using the comparison operator, > @user.protected_profile == true.If this is an ActiveRecord model with a *database* type of boolean, you shouldn''t assume anything about the actual value of the attribute. Different database adapters will use different values. As Fred says, because the value *displays* as ''false'' doesn''t mean it is the "false" boolean value. The best way to test a boolean attribute is with a query predicate: if @user.protected_profile? This allows the adapter to translate the database value into a boolean for you... -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Neil Cauldwell
2008-Mar-11 16:26 UTC
Re: Using a boolean attribute with a conditional statement
Thanks guys. The following seems to do the trick; if @user.protected_profile? == true -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---