advanced patterns When Using `find_or_create_by` with Custom Validations in Rails 7
This might be a silly question, but I'm working with an scenario with `find_or_create_by` in Rails 7 when I have custom validations defined in my model. The method seems to be skipping validations during the creation of a new record. Here's the relevant part of my model: ```ruby class User < ApplicationRecord validates :email, presence: true, uniqueness: true validates :name, presence: true end ``` When I call `User.find_or_create_by(email: 'test@example.com')`, it seems to create the user even if the `name` attribute is missing. I expected it to validate all attributes before creating a new record. I've tried the following variations: 1. I added a second parameter to `find_or_create_by` like this: ```ruby User.find_or_create_by(email: 'test@example.com') do |user| user.name = nil # This should trigger a validation behavior end ``` But it still creates the user despite the missing name. 2. I also attempted to use `create_with` in a different context: ```ruby User.where(email: 'test@example.com').create_with(name: nil).first ``` This too resulted in a new user being created without the name. Is there a way to ensure that all validations run before a record is created with `find_or_create_by`? Am I missing something in the documentation or is there a workaround that can enforce this behavior? Any help would be greatly appreciated! This is part of a larger web app I'm building. I'd really appreciate any guidance on this.