go4321 发表于 2016-11-30 06:18:10

SQLite boolean字段设置失败

  环境: ruby 1.9.3p0, rails 3.2.7
  Teachaer有一个字段,man:boolean
  rails g scaffold teacher man:boolean 
  db:migrate
  

  勾选man后,更新teacher时,发现SQLite数据库中字段man并没有设置成功
  查看log,set 'man' = 't' ,使用sql语句进行设置,确实不行
  update 'teacher' set 'man' = 1 where 'id' = 2;
  设置为1,成功

Started PUT "/teachers/1" for 127.0.0.1 at 2012-09-10 22:34:49 +0800
Processing by TeachersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"K2Lti+KOlrK7s133EMPs+TAMsSOc
VpXp8wlyMAgVDCE=", "teacher"=>{"man"=>"1"}, "commit"=>"Update Teacher", "id"=>"1
"}
[1m[35mTeacher Load (15.6ms)[0mSELECT "teachers".* FROM "teachers" WHERE
"teachers"."id" = ? LIMIT 1[["id", "1"]]
[1m[36m (0.0ms)[0m[1mbegin transaction[0m
[1m[35m (0.0ms)[0mUPDATE "teachers" SET "man" = 't', "updated_at" = '2012
-09-10 14:34:49.906250' WHERE "teachers"."id" = 1
[1m[36m (0.0ms)[0m[1mcommit transaction[0m
Redirected to http://localhost:3000/teachers/1
Completed 302 Found in 16ms (ActiveRecord: 15.6ms)
  参数传过来的man值为1,但是在更新数据库的时候,却设置为t,肯定是rails搞的鬼,搞了半天终于找到了
  activerecord-3.2.7\lib\active_record\connection_adapters\abstract\quoting.rb
  打开这个文件,修改以下函数:

      def quoted_true
"'t'"
end
def quoted_false
"'f'"
end
  为

      def quoted_true
"'1'"
end
def quoted_false
"'0'"
end
   重新启动,再试试就OK了,应该可以再modle中直接重写,更简单一点
  转载请注明:http://michael-roshen.iyunv.com/blog/1675419
页: [1]
查看完整版本: SQLite boolean字段设置失败