私が取り組んでいるrails-apiでは、現在実行中の呼び出しを最適化しようとしていますが、.includes機能に問題があります。私はそれがほとんどの状況で動作しているが、それが私がそれをしたい方法で動作していない1つの特定の状況があります。ここでレールにモデルメソッドで使用されていないキャッシュが含まれています
は例です:
Userクラス
class User < ActiveRecord::Base
has_many :images
has_one :active_image, -> { where(images: { active_image: true })}, class_name: 'Image'
has_many :facebook_auth
def get_profile_image
if active_image
active_image.image.url(:profile)
else
facebook = facebook_auth.last
if facebook
"https://graph.facebook.com/#{facebook.provider_user_id}/picture?width=150&height=150"
end
end
nil
end
end
コントローラー:これにより
class UserController < BaseAPIController
def get_user_image
user_id = params[:user_id]
user = User.includes(:active_image, :facebook_auth).find(user_id)
render json: user.get_profile_image
end
end
、私はそれらを呼び出すときよう.includes(:active_image, :facebook_auth)
がデータをキャッシュすることを前提としますget_profile_imageメソッドを呼び出すと、dbコールはそれ以上作成されませんが、そうではありません。私はここで間違って何をしていますか?
おかげで、 チャーリー
active_imageも含める必要があります –
ああ、ちょうどタイプミスです。私の実際のコードでは、私はそれをやっています。私はそれを修正します。 –
'render json:user.get_profile_image'のタイプミスも' render json:user.get_profile_images'です – neydroid