I''m trying to create a model that uses a boolean type, supported by mysql engine. My generate is like so: ./script/generate model Product name:string vendor_id:integer in_stock:boolean However, when I try to play with the console and save products to the database, only products defined with in_stock = true or in_stock = 1 are successfully saved. I can''t seem to save items with a false value. Here''s what I mean:>> p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => true)=> #<Product id:nil, name: "VCR", vendor_id: 1, in_stock: true, created_at....>>> p.save=> true This also works with :in_use => 1 vHowever, all these fail:>> p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => false) >> p.save=> false>> p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => 0) >> p.save=> false>> p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => nil)>> p.save=> false The migration lists the type as t.boolean :in_use, and the database shows the column type as tinyint(1). What am I doing wrong? Thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
On 24 Dec 2008, at 14:37, sa 125 wrote:> > vHowever, all these fail: > >>> p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => false) >>> p.save > => false >>> p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => 0) >>> p.save > => false >>> p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => >>> nil)>> p.save > => false > > The migration lists the type as t.boolean :in_use, and the database > shows the column type as tinyint(1). >That''s normal. Dou have a validation that this is causing the save to fail ? Fred> What am I doing wrong? > > Thanks! > -- > 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 -~----------~----~----~----~------~----~------~--~---
You were right, I had validates_presence_of :name, :vendor_id, :in_stock inside my model. Once I removed it I was able to save false values. How can I still validate a boolean once I allow a user to enter values (even though it''ll be either one or the other, using a checkbox or something)? -- 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 -~----------~----~----~----~------~----~------~--~---
2008/12/24 sa 125 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > You were right, I had validates_presence_of :name, :vendor_id, :in_stock > inside my model. Once I removed it I was able to save false values. How > can I still validate a boolean once I allow a user to enter values (even > though it''ll be either one or the other, using a checkbox or something)? > > -- > Posted via http://www.ruby-forum.com/. > > > >Probably you can have something like that: validates_inclusion_of :in_stock => [true, false] --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---