Skip to content

Conversation

harsh183
Copy link
Contributor

@harsh183 harsh183 commented Oct 7, 2025

Motivation / Background

In the tutorial we use product.inventory_count? to check if the product is in stock. This is relying on implicit rails behavior that can be confusing and hard to reason about for newcomers.

Detail

This change uses product.inventory_count.positive? instead, which is more explicit and easier to understand. I also standardized on using it throughout the tutorial for consistency.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature. - N/A
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included. - N/A

@rails-bot rails-bot bot added the docs label Oct 7, 2025
@harsh183 harsh183 changed the title [Getting Started] Use .positive? for more explicit positive checks rather than implicitly field itself [Getting Started] Use .positive? for more explicit positive checks rather than implicitly field itself [ci skip] Oct 7, 2025
Copy link
Contributor

@MatheusRich MatheusRich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm positive (pun intended) about this change.


```erb
<% if product.inventory_count? %>
<% if product.inventory_count.positive? %>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just double checking that this is never nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll double check in my tutorial code. If it is nil, maybe it's a good time to introduce default column values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I can do &.positive?, that's easier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a real error to worry about. I added a default: 0 when introducing the migration to guard against this. As a bonus, it's another nice rails feature to throw in the getting started tutorial.

@p8
Copy link
Member

p8 commented Oct 8, 2025

This has been discussed before: #55344

@harsh183
Copy link
Contributor Author

harsh183 commented Oct 8, 2025

Nice! Glad to know I wasn't the only person who thought that was a little confusing for beginners.

…ther than implicitly field itself

In the tutorial we use `product.inventory_count?` to check if the product is in stock. This is relying on implicit rails behavior that can be confusing and hard to reason about for newcomers.

This change uses `product.inventory_count.positive?` instead, which is more explicit and easier to understand. I also standardized on using it throughout the tutorial for consistency.
@harsh183 harsh183 force-pushed the getting_started/standardize_on_positive_check branch from 2b24bf5 to c55e428 Compare October 8, 2025 15:15
@rafaelfranca rafaelfranca merged commit bd3265b into rails:main Oct 9, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants