2022-02-01から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…
save,updateの時はcontextが必要だが、valid?,invalid?の時はシンボルで指定する validates :password, presence: true, on: :change_password user.save(context: :change_password) user.valid?(:change_password) 参考 https://spirits.appirits.com/doru…
スラッシュで名前空間を表現する。 config/locales/ja.yml ja: activerecord: attributes: v1/user: name: ユーザー名 参考 https://qiita.com/yutackall/items/9f349101aaefce6043fb