Gem 请问怎么在部分 action 中跳转已登录用户的 sql 查询(devise)

gxlonline · 2020年04月26日 · 最后由 gxlonline 回复于 2020年04月26日 · 3093 次阅读

网站有几个开放的 api 接口,无需用户登录就可获取数据。

在有用户登录状态下(devise),访问 api 接口,会自动执行一条 sql,查找当前用户。

由于 api 接口访问量很大,每次都多执行一次查询,不是太好。

请问怎么跳转,试过退出登录就没有 User Load。

试过几个 skip_before_action 好像都没有用。

Started GET "/api/nodes/pull?name=test" for ::1 at 2020-04-25 23:55:03 +0800
Processing by Api::NodesController#pull as HTML
  Parameters: {"name"=>"test"}
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`created_at` DESC LIMIT 1
  Node Load (0.4ms)  SELECT `nodes`.* FROM `nodes` WHERE `nodes`.`name` = 'test' ORDER BY `nodes`.`updated_at` DESC LIMIT 1
  ↳ app/controllers/api/nodes_controller.rb:19:in `set_node'
Completed 200 OK in 6ms (Views: 0.1ms | ActiveRecord: 0.8ms | Allocations: 2733)
class Api::ApiController < ActionController::Base
  skip_before_action :verify_authenticity_token
end
class Api::NodesController < Api::ApiController
  before_action :set_node

  def pull
    render json: { node: @node }
  end

  private

    def set_node
      @node = Node.find_by_name params[:name]
    end
end

那条 SQL 查询是放在 ActionController::Base 中,还是 Api::ApiController 中的?

088pause 回复

没有人为写过这条查询,应该是 devise 自动加载的吧?

gem 'devise-security' 去掉这个插件就没有了

gxlonline 关闭了讨论。 04月26日 10:42
gxlonline 重新开启了讨论。 04月26日 10:42
gxlonline 关闭了讨论。 04月26日 10:42
需要 登录 后方可回复, 如果你还没有账号请 注册新账号