Rails和SQL直接请求

时间:2017-01-01 14:59:46

标签: sql ruby-on-rails ruby

在rails中我们有Model.find(id_number)请求。我想问的是有没有可能直接向数据库创建请求,例如:SELECT * FROM users WHERE id = 1并替换rails basic Model.where(id: 1)

修改

在我的model.rb中我有:

scope :search_import, -> {includes(:translations)}
default_scope -> {self.search_import}

请求(在rails控制台中)看起来像:

User Load (0.5ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
User::Translation Load (0.6ms)  SELECT `users_translations`.* FROM `users_translations` WHERE `users_translations`.`category_id` IN (1)

但是我不想User::Translation被执行(没有结合我有相同的结果)

1 个答案:

答案 0 :(得分:1)

当然,像是

ActiveRecord::Base.connection.execute("SELECT * FROM users WHERE id = 1")