Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何确保sqlite不是';是否缓存特定的select查询?_Ruby On Rails_Sqlite_Activerecord - Fatal编程技术网

Ruby on rails 如何确保sqlite不是';是否缓存特定的select查询?

Ruby on rails 如何确保sqlite不是';是否缓存特定的select查询?,ruby-on-rails,sqlite,activerecord,Ruby On Rails,Sqlite,Activerecord,我的情况是,我将sqlite与ActiveRecord和Rails一起使用(同样,这是JRuby,因此我实际上使用了jdbcsqlite适配器,以防万一)。现在,我试图在表中插入一行,但前提是不存在其他类似行。因此 unless AttentionSeeker.find(:first, :conditions => {:key_id => key.id, :locale_id => l.id}) item = AttentionSeeker.new(:key_id =>

我的情况是,我将sqlite与ActiveRecord和Rails一起使用(同样,这是JRuby,因此我实际上使用了jdbcsqlite适配器,以防万一)。现在,我试图在表中插入一行,但前提是不存在其他类似行。因此

unless AttentionSeeker.find(:first, :conditions => {:key_id => key.id, :locale_id => l.id})
  item = AttentionSeeker.new(:key_id => key.id, :locale_id => l.id)
  item.save
end
这是日志中生成的输出:

CACHE (0.0ms)   SELECT * FROM attention_seekers WHERE (attention_seekers.key_id = 318 AND attention_seekers.locale_id = 20) 
AttentionSeeker Create (1.0ms)   INSERT INTO attention_seekers (key_id, locale_id) VALUES(318, 20)
CACHE (0.0ms)   SELECT * FROM attention_seekers WHERE (attention_seekers.key_id = 318 AND attention_seekers.locale_id = 20) 
AttentionSeeker Create (2.0ms)   INSERT INTO attention_seekers (key_id, locale_id) VALUES(318, 20)

正如您所看到的,出于某种原因,正在缓存查找,尽管我插入了影响它的元素。我做错了什么?如何停止这种行为?< /p> ,而不是将代码放在控制器中(这是我猜想的),您可能想考虑使用验证,而我认为可以解决这个问题:

class AttentionSeeker < ActiveRecord::Base
    validates_uniqueness_of :key_id, :scope => :locale_id
end

每次通过循环时,它都会给您一个唯一的查询
class AttentionSeeker < ActiveRecord::Base
    validates_uniqueness_of :key_id, :scope => :locale_id
end

每次通过循环时,它都会给您一个唯一的查询

我做了一些挖掘,发现了更多的信息。我的解决方案(使用Mike Buckbee建议的验证-谢谢!):


我做了一些挖掘,发现了更多的信息。我的解决方案(使用Mike Buckbee建议的验证-谢谢!):


架构级别的唯一索引比验证的唯一性更安全


Stephan

模式级别的唯一索引比验证唯一性更安全


Stephan

我很高兴尝试这个,但我看到了完全相同的行为。您是否尝试过将其包装在交易中?(我的第二个建议)我将上面的代码片段包装在一个事务中,但没有任何更改。我很高兴尝试此操作,但我看到了完全相同的行为。您是否尝试将其包装在事务中?(我的第二个建议)我将上面的代码片段打包在一个事务中,但没有任何变化。太棒了,Josh,很高兴你弄明白了。太棒了,Josh,很高兴你弄明白了。
AttentionSeeker.uncached do
  item = AttentionSeeker.new(:key_id => key.id, :locale_id => l.id)
  item.save
end