I have a checkbox tag as follows: <%= f.check_box :receive_offers %> When I view source in the html, I see the value of the checkbox is "1", with the normal hidden field value of "0". However, after posting the form and saving to the database, the Mysql column, which is a TinyInt, is ALWAYS 0. I''ve tried it with true/false values instead, and no luck. Any ideas? Thanks, Blake Miller ----------------------------- www.syntacticsugr.com -- 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 23 November 2010 15:38, Blake Miller <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have a checkbox tag as follows: > <%= f.check_box :receive_offers %> > > When I view source in the html, I see the value of the checkbox is "1", > with the normal hidden field value of "0". However, after posting the > form and saving to the database, the Mysql column, which is a TinyInt, > is ALWAYS 0.First have a look in development.log where you will see the parameter values posted and check that is what you expect. Then if necessary you can break into your controller code using ruby-debug and can inspect the data to work out what is happening. See the Rails Guide on debugging if you are not familiar with this. Colin -- 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.
Frederick Cheung
2010-Nov-23 15:44 UTC
Re: ActiveRecord not getting true value from checkbox
On Nov 23, 3:38 pm, Blake Miller <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I have a checkbox tag as follows: > <%= f.check_box :receive_offers %> > > When I view source in the html, I see the value of the checkbox is "1", > with the normal hidden field value of "0". However, after posting the > form and saving to the database, the Mysql column, which is a TinyInt, > is ALWAYS 0. >Try and find at which point things go wrong, eg does the controller get the right params? Donjon have attr_protected blocking mass assignment of that attribute? Is some other piece of code overwriting the value supplied by the form Fred> I''ve tried it with true/false values instead, and no luck. > > Any ideas? > > Thanks, > Blake Miller > -----------------------------www.syntacticsugr.com > > -- > Posted viahttp://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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I think you''re right...it''s probably somewhere in the save code. Problem is, the save method is buried in the Devise (gem) code, so have to figure out what''s going on there :/. I''ve verified before and after the save method, the value is 1; however, the value in the raw sql insert query into the database is 0. -- 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 23 November 2010 16:56, Blake Miller <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: Please remember to quote the previous message so one can follow the thread> I think you''re right...it''s probably somewhere in the save code.Who?> Problem is, the save method is buried in the Devise (gem) code, so have > to figure out what''s going on there :/. I''ve verified before and after > the save method, the value is 1; however, the value in the raw sql > insert query into the database is 0.Are you sure the attribute name exactly matches the column name - check for typo. Check that mass write is not prevented (as Marnen suggested) by creating, setting and saving a record in the console. Colin -- 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.
Blake Miller
2010-Nov-23 19:16 UTC
Re: Re: ActiveRecord not getting true value from checkbox
Colin Law wrote in post #963375:> On 23 November 2010 16:56, Blake Miller <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > Please remember to quote the previous message so one can follow the > thread > >> I think you''re right...it''s probably somewhere in the save code. > > Who? > >> Problem is, the save method is buried in the Devise (gem) code, so have >> to figure out what''s going on there :/. I''ve verified before and after >> the save method, the value is 1; however, the value in the raw sql >> insert query into the database is 0. > > Are you sure the attribute name exactly matches the column name - > check for typo. > Check that mass write is not prevented (as Marnen suggested) by > creating, setting and saving a record in the console. > > ColinI got it figured out... I had an attr_accessor set for the :receive_offers field, AND the column existed on the table. As such, when the AR object was populated (from the form hash), it was setting the attribute from attr_accessor, rather than the one from the database. Not a very clear explanation, but removing the unnecessary attr_accessor was the culprit. Thanks for your help -- 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.