带有rails活动记录的sqlite boolean't'和'f'

时间:2014-04-22 15:29:28

标签: ruby-on-rails-4 sqlite

我在开发时使用mysql和sqlite3。

在开发时查询我的数据库,例如

@follow_ups = FollowUp.where(is_complete: false)

我在控制台中获取了以下sql

SELECT "follow_ups".* FROM "follow_ups"  WHERE "follow_ups"."is_complete" = 'f'

sqlite评估' f'作为一个truthy值,所以不返回follow_ups.is_complete = false。在数据库中,它们存储为true / false。根据我的调查,我找到了。

https://github.com/rails/rails/issues/10720
Rails 3 SQLite3 Boolean false

如何让布尔过滤器工作?我原本以为这会发生在更多人身上。

见下面的架构

  create_table "follow_ups", force: true do |t|
    t.integer  "contact_id"
    t.integer  "owner_id"
    t.datetime "due_date"
    t.string   "comment"
    t.boolean  "is_complete",        default: false
    t.integer  "created_by_user_id"
    t.integer  "updated_by_user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

请参阅下面的数据和插入内容 - 通过rails控制台完成。作为评论中的要求。

[30] pry(main)> FollowUp.create(contact_id: 1, due_date: Time.now, is_complete: true)
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "follow_ups" ("contact_id", "created_at", "due_date", "is_complete", "updated_at") VALUES (?, ?, ?, ?, ?)  [["contact_id", 1], ["created_at", "2014-04-22 19:17:01.854402"], ["due_date", "2014-04-22 19:17:01.853540"], ["is_complete", "t"], ["updated_at", "2014-04-22 19:17:01.854402"]]
   (1.7ms)  commit transaction
#<FollowUp:0x0000010699c5a8> {
                    :id => 23,
            :contact_id => 1,
              :owner_id => nil,
              :due_date => Tue, 22 Apr 2014 19:17:01 UTC +00:00,
               :comment => nil,
           :is_complete => true,
    :created_by_user_id => nil,
    :updated_by_user_id => nil,
            :created_at => Tue, 22 Apr 2014 19:17:01 UTC +00:00,
            :updated_at => Tue, 22 Apr 2014 19:17:01 UTC +00:00
}
[31] pry(main)> FollowUp.where(is_complete: true)
  FollowUp Load (0.3ms)  SELECT "follow_ups".* FROM "follow_ups"  WHERE "follow_ups"."is_complete" = 't'
#<ActiveRecord::Relation [#<FollowUp id: 16, contact_id: 1, owner_id: 1, due_date: "2014-04-23 00:00:00", comment: "Lorem ipsum dolor sit amet, consectetur adipisicin...", is_complete: true, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-17 09:57:00", updated_at: "2014-04-22 14:37:36">, #<FollowUp id: 23, contact_id: 1, owner_id: nil, due_date: "2014-04-22 19:17:01", comment: nil, is_complete: true, created_by_user_id: nil, updated_by_user_id: nil, created_at: "2014-04-22 19:17:01", updated_at: "2014-04-22 19:17:01">]>
[32] pry(main)> FollowUp.where(is_complete: false)
  FollowUp Load (0.2ms)  SELECT "follow_ups".* FROM "follow_ups"  WHERE "follow_ups"."is_complete" = 'f'
#<ActiveRecord::Relation []>
[33] pry(main)> FollowUp.all
  FollowUp Load (0.2ms)  SELECT "follow_ups".* FROM "follow_ups"
#<ActiveRecord::Relation [#<FollowUp id: 16, contact_id: 1, owner_id: 1, due_date: "2014-04-23 00:00:00", comment: "Lorem ipsum dolor sit amet, consectetur adipisicin...", is_complete: true, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-17 09:57:00", updated_at: "2014-04-22 14:37:36">, #<FollowUp id: 17, contact_id: 1, owner_id: 1, due_date: "2014-04-24 00:00:00", comment: "This ia  long comment", is_complete: false, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-17 10:04:13", updated_at: "2014-04-17 10:04:13">, #<FollowUp id: 18, contact_id: 1, owner_id: 1, due_date: "2014-04-24 00:00:00", comment: "This is a comment\r\n", is_complete: false, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-17 10:24:05", updated_at: "2014-04-17 10:24:05">, #<FollowUp id: 19, contact_id: 1, owner_id: 1, due_date: "2014-04-23 00:00:00", comment: "test", is_complete: false, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-22 13:37:40", updated_at: "2014-04-22 13:37:40">, #<FollowUp id: 20, contact_id: 1, owner_id: 1, due_date: "2014-04-23 00:00:00", comment: "test", is_complete: false, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-22 13:37:41", updated_at: "2014-04-22 13:37:41">, #<FollowUp id: 21, contact_id: 1, owner_id: 1, due_date: "2014-04-24 00:00:00", comment: "test", is_complete: false, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-22 13:39:20", updated_at: "2014-04-22 13:39:20">, #<FollowUp id: 22, contact_id: 2, owner_id: 1, due_date: "2014-04-30 00:00:00", comment: "test", is_complete: false, created_by_user_id: 1, updated_by_user_id: 1, created_at: "2014-04-22 13:53:37", updated_at: "2014-04-22 13:53:37">, #<FollowUp id: 23, contact_id: 1, owner_id: nil, due_date: "2014-04-22 19:17:01", comment: nil, is_complete: true, created_by_user_id: nil, updated_by_user_id: nil, created_at: "2014-04-22 19:17:01", updated_at: "2014-04-22 19:17:01">]>

2 个答案:

答案 0 :(得分:5)

在sqlite3中,不支持布尔值。因此rails使用char't'和'f'来表示布尔值true和false。而在其他数据库如mysql,postgresql中,使用了真实的布尔值true和false。

但是,这种差异对rails Model是透明的。你可以使用

FollowUp.where(is_compete: true)

过滤完成的后续内容和

FollowUp.where(is_compete: false)

获得不完整的后续行动

答案 1 :(得分:1)

在Rails 4.2.5中使用sqlite3,我发现从Rails控制台查询时,boolean字段返回ruby true或false。但是,在控制器方法中查询时,它返回“t”或“f”字符串。很奇怪。所以我添加了一个模型方法:

def fixt?
    return self[:fixt] == "t"
end

架构:

 t.boolean  "fixt",                  default: false