これらのモデルを想像してください:
class User
belongs_to :profile
# has email here
end
class Profile
has_one :user
# has first_name,last_name
end
と
class Post
belongs_to :profile
# has title,content
end
ここで、ユーザーの電子メールですべての投稿を照会したいと思います (LIKE "%substring%" を実行します)。かなり非効率的なコードが生成されると思うので、map/selects を使用して記述する必要はありません。私はそのようなことを試しました:
class Post
def self.with_user_email_like(email)
self.joins(:profile).where("profile.email LIKE ?","%#{email}%")
end
end
問題は、上記の条件で profile.user.email を使用する必要があることをどういうわけか知っていますが、それを機能させることができません。助言がありますか?