使用Rails 3.1和sqlite3进行开发,测试环境。sqlite3 varchar匹配“like”但不匹配“=”
增加了一个新的表中迁移:
create_table :api_keys do |t|
t.string :api_key
t.integer :user_id
t.timestamps
end
这将产生一个表,下面的模式:
create_table "api_keys", :force => true do |t|
t.string "api_key"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
在ActiveRecord的模式:
before_create :fill_api_key
private
def fill_api_key
self.api_key = SecureRandom.hex(30)
end
的ActiveRecord的动态查找方法find_by_api_key(api_key)
不起作用(返回零)。同样的,:
ApiKey.where({:api_key => 'something'}).first
在sqlite3的,我做了以下内容:
insert into api_keys (id, api_key) values (-1, '12345');
如果我现在运行一个选择:
select api_keys.* from api_keys where api_keys.api_key = '12345';
的记录会被发现。如果我运行从我的应用程序创建
预先存在的数据显示一个未经过滤的选择:
select api_keys.* from api_keys;
如果我尝试从一个粘贴到我的查询很长的十六进制字符串找到预先存在的记录那些预先存在的记录:
select api_keys.* from api_keys where api_keys.api_key = 'long hex string';
然后它不返回任何结果。如果我试试这个:
select api_keys.* from api_keys where api_keys.api_key like 'long hex string';
然后我得到一个匹配。
我在api_keys.api_key上创建了一个索引,但没有任何效果。
此问题会影响我的应用程序中的另一个模型,该模型会使用Digest :: SHA1 :: hexdigest生成类似的随机十六进制数字串。
詹姆斯
我要补充一点,这个工作对我提到的,直到升级到Rails 3.1的第二个模型类。测试在那时开始失败。 –