2022-02-08から1日間の記事一覧
class Post < ApplicationRecord scope :latest_post, ->(user_id) do where(user_id: user_id).last end end Post.latest_post(1) =>すべてのレコードが返る Post.where(user_id: user_id)でnilが返る場合scopeはallを返すような挙動になるので注意。 where…
class Post < ApplicationRecord scope :latest_post, ->(user_id) do where(user_id: user_id).last end end Post.latest_post(1) =>すべてのレコードが返る Post.where(user_id: user_id)でnilが返る場合scopeはallを返すような挙動になるので注意。 where…