Skip to content

Add find_or_create_by and find_or_create_by! with block support#2

Draft
Copilot wants to merge 6 commits into
mainfrom
copilot/add-find-or-create-by-method
Draft

Add find_or_create_by and find_or_create_by! with block support#2
Copilot wants to merge 6 commits into
mainfrom
copilot/add-find-or-create-by-method

Conversation

Copy link
Copy Markdown

Copilot AI commented Dec 9, 2025

Implements ActiveRecord-style find_or_create_by and find_or_create_by! for finding existing records or creating new ones, with full support for Rails-style block yielding.

Changes

  • Base class methods: Added create(attributes, &block) and create!(attributes, &block) for creating and saving records. create! raises RecordInvalid on validation failure. Both methods accept an optional block that gets yielded to the new record before saving.
  • Scoping module: Added find_or_create_by(conditions, &block) and find_or_create_by!(conditions, &block). The bang version raises on validation errors. Both methods accept an optional block that only executes when creating (not when finding existing records).
  • Version: Bumped to 0.3.0 for new backward-compatible features.

Usage

# Returns existing record or creates new one
user = User.find_or_create_by(email: "alice@example.com")

# Raises RecordInvalid if creation fails validation
user = User.find_or_create_by!(email: "alice@example.com", name: "Alice")

# Block yielding (Rails-style) - only runs when creating new record
user = User.find_or_create_by(email: "alice@example.com") do |u|
  u.first_name = "Alice"
  u.last_name = "Smith"
end

Block Yielding

The block parameter works exactly like Rails' ActiveRecord:

  • Block receives the new record before it's saved
  • Block only executes if the record is being created (not found)
  • You can set additional attributes or run custom logic in the block

Implementation notes

Uses traditional find-then-create approach (2 API calls) rather than Airtable's performUpsert. Upsert would update existing records, which violates find_or_create_by semantics. Field mappings work correctly in both find and create operations.

Original prompt

This section details on the original issue you should resolve

<issue_title>we need #find_or_create_by{,!}!</issue_title>
<issue_description></issue_description>

<agent_instructions>make it happen, cap'n</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits December 9, 2025 07:48
Co-authored-by: 24c02 <163450896+24c02@users.noreply.github.com>
Co-authored-by: 24c02 <163450896+24c02@users.noreply.github.com>
Co-authored-by: 24c02 <163450896+24c02@users.noreply.github.com>
Copilot AI changed the title [WIP] Add find_or_create_by method functionality Add find_or_create_by and find_or_create_by! Dec 9, 2025
Copilot AI requested a review from 24c02 December 9, 2025 07:56
@24c02
Copy link
Copy Markdown
Owner

24c02 commented Dec 9, 2025

@copilot what about yielding like rails?

Copilot AI and others added 2 commits December 9, 2025 13:23
Co-authored-by: 24c02 <163450896+24c02@users.noreply.github.com>
Co-authored-by: 24c02 <163450896+24c02@users.noreply.github.com>
Copy link
Copy Markdown
Author

Copilot AI commented Dec 9, 2025

@copilot what about yielding like rails?

Added block yielding support in commit 68918bd. Both find_or_create_by and find_or_create_by! now accept a block that gets yielded to the new record only when creating (not when finding existing records). Version bumped to 0.3.0.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.airtable.com
    • Triggering command: /home/REDACTED/.local/share/gem/ruby/3.2.0/bin/rspec rspec --format documentation (dns block)
    • Triggering command: /home/REDACTED/.local/share/gem/ruby/3.2.0/bin/rspec rspec --format progress (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Add find_or_create_by and find_or_create_by! Add find_or_create_by and find_or_create_by! with block support Dec 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

we need #find_or_create_by{,!}!

2 participants