在Rails(3.2)的应用程序,我有这样一个模型类方法:如何将此原始SQL转换为Arel查询?
def import(level, max = 10)
db = ActiveRecord::Base.connection
result = db.execute("SELECT word FROM levels WHERE level == #{level} AND word NOT IN (SELECT entry FROM words) limit #{max};");
它只是在不为Word记录还不存在一个时间导入10个新词(创建10条记录) 。
架构看起来是这样的:
create_table "levels", :force => true do |t|
t.string "word"
t.integer "level"
end
create_table "words", :force => true do |t|
t.string "entry"
t.integer "level", :default => 0
t.text "definition"
t.string "thesaurus", :default => "none"
end
我是SQL菜鸟。使用rails dbconsole(sqlite3,我在服务器上也使用sqlite3),我以某种方式想出了上面的原始sql查询。我有点知道我可以用Arel做同样的事情。我应该如何用ActiveRecord构造查询?
谢谢!我明白了,但是'Level.where(“word not in?”,“apple,banana”)'产生:'SQLite3 :: SQLException:no such table:apple,banana:SELECT“levels”。* FROM“levels “在哪里(不是'苹果,香蕉'的单词)'嗯。 – knsmr 2012-04-16 10:14:06
我想如果你在最后拿出'join(“,”)'它应该可以工作。 'IN'插值需要一个数组。 – tsherif 2012-04-16 10:18:20
是的,我只是想出了自己。 (Level:> level).where(“word not IN(?)”,Word.pluck(:entry))。limit(max)'作品。你也需要'()'。非常感谢! – knsmr 2012-04-16 10:27:43