Kevin Clark
2005-Dec-21 20:55 UTC
Checkboxes evaluate to 1 and 0 (not boolean), is this a bug?
I''m working on an application using sqlite locally and recently noticed that checkboxes are evaluating to 1 and 0 (not true and false) and thus aren''t being sanitized to boolean values in the db (''t'' and ''f'' in sqlite). This isn''t a problem in mysql because 1 and 0 are synonymous to true and false, but there''s issues for some. Is this a bug? Is there some way around it that I''ve missed somewhere? Kev
David Rupp
2005-Dec-21 21:01 UTC
Re: Checkboxes evaluate to 1 and 0 (not boolean), is this a bug?
Specify a checked_value and unchecked_value in your check_box method call. Here''s the method signature from the API docs (api.rubyonrails.com): check_box(object, method, options = {}, checked_value = "1", unchecked_value = "0") So you could replace the defaults of "1" and "0" with "t" and "f", for example. Regards, David On Dec 21, 2005, at 1:55 PM, Kevin Clark wrote:> I''m working on an application using sqlite locally and recently > noticed that checkboxes are evaluating to 1 and 0 (not true and false) > and thus aren''t being sanitized to boolean values in the db (''t'' and > ''f'' in sqlite). This isn''t a problem in mysql because 1 and 0 are > synonymous to true and false, but there''s issues for some. > > Is this a bug? Is there some way around it that I''ve missed somewhere? > > Kev > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core
Kevin Clark
2005-Dec-21 21:31 UTC
Re: Checkboxes evaluate to 1 and 0 (not boolean), is this a bug?
Replacing the defaults with t and f would work for sqlite, but it isn''t database agnostic. Is there a way to do it and maintain cross db functionality? On 12/21/05, David Rupp <rails@ruppconsulting.com> wrote:> Specify a checked_value and unchecked_value in your check_box method > call. Here''s the method signature from the API docs > (api.rubyonrails.com): > > check_box(object, method, options = {}, checked_value = "1", > unchecked_value = "0") > > So you could replace the defaults of "1" and "0" with "t" and "f", > for example. > > Regards, > David > > On Dec 21, 2005, at 1:55 PM, Kevin Clark wrote: > > > I''m working on an application using sqlite locally and recently > > noticed that checkboxes are evaluating to 1 and 0 (not true and false) > > and thus aren''t being sanitized to boolean values in the db (''t'' and > > ''f'' in sqlite). This isn''t a problem in mysql because 1 and 0 are > > synonymous to true and false, but there''s issues for some. > > > > Is this a bug? Is there some way around it that I''ve missed somewhere? > > > > Kev > > _______________________________________________ > > Rails-core mailing list > > Rails-core@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >
Tobias Luetke
2005-Dec-21 21:49 UTC
Re: Checkboxes evaluate to 1 and 0 (not boolean), is this a bug?
maybe we should add connection.false / connection.true methods for such things to AbstractAdapter? On 12/21/05, Kevin Clark <kevin.clark@gmail.com> wrote:> Replacing the defaults with t and f would work for sqlite, but it > isn''t database agnostic. Is there a way to do it and maintain cross db > functionality? > > On 12/21/05, David Rupp <rails@ruppconsulting.com> wrote: > > Specify a checked_value and unchecked_value in your check_box method > > call. Here''s the method signature from the API docs > > (api.rubyonrails.com): > > > > check_box(object, method, options = {}, checked_value = "1", > > unchecked_value = "0") > > > > So you could replace the defaults of "1" and "0" with "t" and "f", > > for example. > > > > Regards, > > David > > > > On Dec 21, 2005, at 1:55 PM, Kevin Clark wrote: > > > > > I''m working on an application using sqlite locally and recently > > > noticed that checkboxes are evaluating to 1 and 0 (not true and false) > > > and thus aren''t being sanitized to boolean values in the db (''t'' and > > > ''f'' in sqlite). This isn''t a problem in mysql because 1 and 0 are > > > synonymous to true and false, but there''s issues for some. > > > > > > Is this a bug? Is there some way around it that I''ve missed somewhere? > > > > > > Kev > > > _______________________________________________ > > > Rails-core mailing list > > > Rails-core@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > _______________________________________________ > > Rails-core mailing list > > Rails-core@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >-- Tobi http://jadedpixel.com - modern e-commerce software http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog
Kevin Clark
2005-Dec-22 04:29 UTC
Re: Checkboxes evaluate to 1 and 0 (not boolean), is this a bug?
After looking at the AR code, I think this can be solved in SQLiteAdapter::quote. I''m writing up a patch now. Kev On 12/21/05, Tobias Luetke <tobias.luetke@gmail.com> wrote:> maybe we should add connection.false / connection.true methods for > such things to AbstractAdapter? > > On 12/21/05, Kevin Clark <kevin.clark@gmail.com> wrote: > > Replacing the defaults with t and f would work for sqlite, but it > > isn''t database agnostic. Is there a way to do it and maintain cross db > > functionality? > > > > On 12/21/05, David Rupp <rails@ruppconsulting.com> wrote: > > > Specify a checked_value and unchecked_value in your check_box method > > > call. Here''s the method signature from the API docs > > > (api.rubyonrails.com): > > > > > > check_box(object, method, options = {}, checked_value = "1", > > > unchecked_value = "0") > > > > > > So you could replace the defaults of "1" and "0" with "t" and "f", > > > for example. > > > > > > Regards, > > > David > > > > > > On Dec 21, 2005, at 1:55 PM, Kevin Clark wrote: > > > > > > > I''m working on an application using sqlite locally and recently > > > > noticed that checkboxes are evaluating to 1 and 0 (not true and false) > > > > and thus aren''t being sanitized to boolean values in the db (''t'' and > > > > ''f'' in sqlite). This isn''t a problem in mysql because 1 and 0 are > > > > synonymous to true and false, but there''s issues for some. > > > > > > > > Is this a bug? Is there some way around it that I''ve missed somewhere? > > > > > > > > Kev > > > > _______________________________________________ > > > > Rails-core mailing list > > > > Rails-core@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > > _______________________________________________ > > > Rails-core mailing list > > > Rails-core@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > _______________________________________________ > > Rails-core mailing list > > Rails-core@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > -- > Tobi > http://jadedpixel.com - modern e-commerce software > http://typo.leetsoft.com - Open source weblog engine > http://blog.leetsoft.com - Technical weblog >
Kevin Clark
2005-Dec-22 06:44 UTC
Re: Checkboxes evaluate to 1 and 0 (not boolean), is this a bug?
After some hacking, I don''t think this can be solved by writing SQLiteAdapter::quote. I did however come up with a failing test case (fails on sqlite, passes on mysql). Still hacking away. Here''s the diff: Index: base_test.rb ==================================================================--- base_test.rb (revision 3321) +++ base_test.rb (working copy) @@ -921,8 +921,14 @@ def test_quote author_name = "\\ \001 '' \n \\n \"" - topic = Topic.create(''author_name'' => author_name) + topic = Topic.create(''author_name'' => author_name, ''approved'' => false) assert_equal author_name, Topic.find(topic.id).author_name + + # Simulates true field from checkbox, needs to be quoted + params = {"topic"=>{"approved"=>"1"}} + + Topic.update_all ["approved = ?",params["topic"]["approved"]], "id = #{topic.id}" + assert !Topic.find(:first, :conditions => [''id = ? and approved ?'', topic.id, true]).nil? end def test_class_level_destroy On 12/21/05, Kevin Clark <kevin.clark@gmail.com> wrote:> After looking at the AR code, I think this can be solved in > SQLiteAdapter::quote. I''m writing up a patch now. > Kev > > On 12/21/05, Tobias Luetke <tobias.luetke@gmail.com> wrote: > > maybe we should add connection.false / connection.true methods for > > such things to AbstractAdapter? > > > > On 12/21/05, Kevin Clark <kevin.clark@gmail.com> wrote: > > > Replacing the defaults with t and f would work for sqlite, but it > > > isn''t database agnostic. Is there a way to do it and maintain cross db > > > functionality? > > > > > > On 12/21/05, David Rupp <rails@ruppconsulting.com> wrote: > > > > Specify a checked_value and unchecked_value in your check_box method > > > > call. Here''s the method signature from the API docs > > > > (api.rubyonrails.com): > > > > > > > > check_box(object, method, options = {}, checked_value = "1", > > > > unchecked_value = "0") > > > > > > > > So you could replace the defaults of "1" and "0" with "t" and "f", > > > > for example. > > > > > > > > Regards, > > > > David > > > > > > > > On Dec 21, 2005, at 1:55 PM, Kevin Clark wrote: > > > > > > > > > I''m working on an application using sqlite locally and recently > > > > > noticed that checkboxes are evaluating to 1 and 0 (not true and false) > > > > > and thus aren''t being sanitized to boolean values in the db (''t'' and > > > > > ''f'' in sqlite). This isn''t a problem in mysql because 1 and 0 are > > > > > synonymous to true and false, but there''s issues for some. > > > > > > > > > > Is this a bug? Is there some way around it that I''ve missed somewhere? > > > > > > > > > > Kev > > > > > _______________________________________________ > > > > > Rails-core mailing list > > > > > Rails-core@lists.rubyonrails.org > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > > > > _______________________________________________ > > > > Rails-core mailing list > > > > Rails-core@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > > > _______________________________________________ > > > Rails-core mailing list > > > Rails-core@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > > > > > -- > > Tobi > > http://jadedpixel.com - modern e-commerce software > > http://typo.leetsoft.com - Open source weblog engine > > http://blog.leetsoft.com - Technical weblog > > >
Kevin Clark
2005-Dec-22 19:36 UTC
Re: Checkboxes evaluate to 1 and 0 (not boolean), is this a bug?
Any updates on this? Tobi, how would connection.true/false work? From what I understand, the problem is that when true/false values come in as strings ("0", "1") from a checkbox, they aren''t changed to "t'' and "f" as needed. My only thoughts on how to decide whether they need to be converted is based on the column type, which in SQLiteAdapter::quote isn''t always passed. On 12/21/05, Kevin Clark <kevin.clark@gmail.com> wrote:> After some hacking, I don''t think this can be solved by writing > SQLiteAdapter::quote. I did however come up with a failing test case > (fails on sqlite, passes on mysql). Still hacking away. Here''s the > diff: > > Index: base_test.rb > ==================================================================> --- base_test.rb (revision 3321) > +++ base_test.rb (working copy) > @@ -921,8 +921,14 @@ > > def test_quote > author_name = "\\ \001 '' \n \\n \"" > - topic = Topic.create(''author_name'' => author_name) > + topic = Topic.create(''author_name'' => author_name, ''approved'' => false) > assert_equal author_name, Topic.find(topic.id).author_name > + > + # Simulates true field from checkbox, needs to be quoted > + params = {"topic"=>{"approved"=>"1"}} > + > + Topic.update_all ["approved = ?",params["topic"]["approved"]], > "id = #{topic.id}" > + assert !Topic.find(:first, :conditions => [''id = ? and approved > ?'', topic.id, true]).nil? > end > > def test_class_level_destroy > > > On 12/21/05, Kevin Clark <kevin.clark@gmail.com> wrote: > > After looking at the AR code, I think this can be solved in > > SQLiteAdapter::quote. I''m writing up a patch now. > > Kev > > > > On 12/21/05, Tobias Luetke <tobias.luetke@gmail.com> wrote: > > > maybe we should add connection.false / connection.true methods for > > > such things to AbstractAdapter? > > > > > > On 12/21/05, Kevin Clark <kevin.clark@gmail.com> wrote: > > > > Replacing the defaults with t and f would work for sqlite, but it > > > > isn''t database agnostic. Is there a way to do it and maintain cross db > > > > functionality? > > > > > > > > On 12/21/05, David Rupp <rails@ruppconsulting.com> wrote: > > > > > Specify a checked_value and unchecked_value in your check_box method > > > > > call. Here''s the method signature from the API docs > > > > > (api.rubyonrails.com): > > > > > > > > > > check_box(object, method, options = {}, checked_value = "1", > > > > > unchecked_value = "0") > > > > > > > > > > So you could replace the defaults of "1" and "0" with "t" and "f", > > > > > for example. > > > > > > > > > > Regards, > > > > > David > > > > > > > > > > On Dec 21, 2005, at 1:55 PM, Kevin Clark wrote: > > > > > > > > > > > I''m working on an application using sqlite locally and recently > > > > > > noticed that checkboxes are evaluating to 1 and 0 (not true and false) > > > > > > and thus aren''t being sanitized to boolean values in the db (''t'' and > > > > > > ''f'' in sqlite). This isn''t a problem in mysql because 1 and 0 are > > > > > > synonymous to true and false, but there''s issues for some. > > > > > > > > > > > > Is this a bug? Is there some way around it that I''ve missed somewhere? > > > > > > > > > > > > Kev > > > > > > _______________________________________________ > > > > > > Rails-core mailing list > > > > > > Rails-core@lists.rubyonrails.org > > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > > > > > > _______________________________________________ > > > > > Rails-core mailing list > > > > > Rails-core@lists.rubyonrails.org > > > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > > > > > _______________________________________________ > > > > Rails-core mailing list > > > > Rails-core@lists.rubyonrails.org > > > > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > > > > > > > > > > > > -- > > > Tobi > > > http://jadedpixel.com - modern e-commerce software > > > http://typo.leetsoft.com - Open source weblog engine > > > http://blog.leetsoft.com - Technical weblog > > > > > >