How to eager load associations with the current_user?

前端 未结 4 756
暖寄归人
暖寄归人 2021-02-07 05:33

I\'m using Devise for authentication in my Rails app. I\'d like to eager load some of a users associated models in some of my controllers. Something like this:

c         


        
4条回答
  •  一生所求
    2021-02-07 05:55

    Override serialize_from_session in your User model.

    class User
      devise :database_authenticatable
    
      def self.serialize_from_session key, salt
        record = where(id: key).eager_load(:saved_listings, roles: :accounts).first
        record if record && record.authenticatable_salt == salt
      end
    end
    

    This will however, eager load on all requests.

提交回复
热议问题