Hi! I have a form with a checkbox called "status" which is a boolean in the database. In my controller''s update action I want to check if the checkbox has changed. Something like this: if @item.status != params[:item][:status] params[:item][:position] = nil end But this doesn''t work as the params[:item][:status] is "0" (an int as a string) And the @item.status is false/true (a bool) I have tried to use the .to_i on the status param to make it into an int, but it still doesn''t work. I guess there is no .to_bool? :) So, how do I do this comparison? I''m new to Rails and these small things makes be a bit puzzled :) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/P7BRyWdhMwwJ. 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.
Tried to use .to_i on the both? 2011/7/25 Linus Pettersson <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> Hi! > > I have a form with a checkbox called "status" which is a boolean in the > database. > > In my controller''s update action I want to check if the checkbox has > changed. Something like this: > > if @item.status != params[:item][:status] > params[:item][:position] = nil > end > > But this doesn''t work as the params[:item][:status] is "0" (an int as a > string) > And the @item.status is false/true (a bool) > > I have tried to use the .to_i on the status param to make it into an int, > but it still doesn''t work. I guess there is no .to_bool? :) > > So, how do I do this comparison? I''m new to Rails and these small things > makes be a bit puzzled :) > > > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/P7BRyWdhMwwJ. > 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. >-- Fernando Almeida www.fernandoalmeida.net -- 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.
Yes, it gives me this error: NoMethodError (undefined method `to_i'' for false:FalseClass): -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/aQi0DLBhzIQJ. 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.
Try @item.status != !params[:item][:status].to_i.zero? 2011/7/25 Linus Pettersson <linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> Yes, it gives me this error: > NoMethodError (undefined method `to_i'' for false:FalseClass): > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/aQi0DLBhzIQJ. > 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. >-- Fernando Almeida www.fernandoalmeida.net -- 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.
Yes, ended up doing this: !@item.status != params[:item][:status].to_i.zero? But it looks very ugly to me... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/z1tqfT3S7FAJ. 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.
after update do if @item.status_changed? ... On Jul 25, 2011, at 16:43 , Linus Pettersson wrote:> Hi! > > I have a form with a checkbox called "status" which is a boolean in the database. > > In my controller''s update action I want to check if the checkbox has changed. Something like this: > > if @item.status != params[:item][:status] > params[:item][:position] = nil > end > > But this doesn''t work as the params[:item][:status] is "0" (an int as a string) > And the @item.status is false/true (a bool) > > I have tried to use the .to_i on the status param to make it into an int, but it still doesn''t work. I guess there is no .to_bool? :) > > So, how do I do this comparison? I''m new to Rails and these small things makes be a bit puzzled :) > > > > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/P7BRyWdhMwwJ. > 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.-- ==============================================================================Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz ============================================================================== -- 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.
Hmm, that seems to always return false over here. I tried this: update_was_successful = @item.update_attributes(params[:item]) puts @item.status_changed? Which outputs false, always. The weird thing is that it prints that before the update is shown in the console. I guess that is why it''s always false. Isn''t the update_attributes method updating the item right away? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1f8S0VjnS4IJ. 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.
Since the params will give you either "0" or "1" as a string, and since you are trying to equate "0" (string) to "false" (boolean), why not set a variable equal to "true" or "false" based on the params? Try: if params[:item][:status] == "0" bool = false else bool = true end or, bool = (params[:item][:status] != "0") You can then compare bool to your boolean. On Jul 25, 1:54 pm, Linus Pettersson <linus.petters...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmm, that seems to always return false over here. > I tried this: > update_was_successful = @item.update_attributes(params[:item]) > puts @item.status_changed? > > Which outputs false, always. The weird thing is that it prints that before > the update is shown in the console. I guess that is why it''s always false. > > Isn''t the update_attributes method updating the item right away?-- 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.
Yes, but that doesn''t look very good either. Not much better than doing this I''m afraid: !@item.status != params[:item][:status].to_i.zero? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/5WTTvhf88cIJ. 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 Mon, Jul 25, 2011 at 11:08 AM, Sandy <sandy.asman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Since the params will give you either "0" or "1" as a string, and > since you are trying to equate "0" (string) to "false" (boolean), why > not set a variable equal to "true" or "false" based on the params?Or put something like this in an initializer: -------------------- class String def true? ( self == "1" || self == "true" ) ? true : false end def false? !self.true? end end -------------------- Then you can more readably check e.g. params[:item][:status].true? FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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 Jul 25, 2:32 pm, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Jul 25, 2011 at 11:08 AM, Sandy <sandy.as...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Since the params will give you either "0" or "1" as a string, and > > since you are trying to equate "0" (string) to "false" (boolean), why > > not set a variable equal to "true" or "false" based on the params? > > Or put something like this in an initializer: > -------------------- > class String > > def true? > ( self == "1" || self == "true" ) ? true : false > end > > def false? > !self.true? > end > > end > -------------------- > > Then you can more readably check e.g. params[:item][:status].true? > > FWIW, > -- > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe80Tx58lXaADg@public.gmane.org://about.me/hassanschroeder > twitter: @hassanOr this may be helpful: def convert_to_boolean(value) return [true, "true", 1, "1", "T", "t"].include?(value.class =String ? value.downcase : value) end -- 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.
On Jul 25, 10:43 am, Linus Pettersson <linus.petters...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > I have a form with a checkbox called "status" which is a boolean in the > database. > > In my controller''s update action I want to check if the checkbox has > changed.No, you really shouldn''t be doing that in the controller. You almost certainly want to do this in a before_save (or possibly before_validation) callback on the MODEL. Several reasons: - it''s a best practice to keep business logic in the model ("fat model / skinny controller") - inside the model callbacks, you''ve got access to both typecast data (the incoming status will already have the right type) and the _changed? predicate. For instance, your code could be shortened to: class Item < ActiveRecord::Base before_save :update_position def update_position self.position = nil if status_changed? end end Not only does it avoid the messy conversion, it (IMHO) is far more readable. --Matt Jones -- 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.
That is very true. Thank you for that! I ended up changing the behavior though, so I didn''t need to check if the status had changed at all. Best Regards Linus -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/L50TpLD2K00J. 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.