https://github.com/remy727/ruby-pro-tips
Ruby Pro Tips
https://github.com/remy727/ruby-pro-tips
rails ruby tips
Last synced: 4 months ago
JSON representation
Ruby Pro Tips
- Host: GitHub
- URL: https://github.com/remy727/ruby-pro-tips
- Owner: remy727
- Created: 2024-03-15T08:26:41.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-28T16:39:28.000Z (5 months ago)
- Last Synced: 2025-01-28T17:35:21.622Z (5 months ago)
- Topics: rails, ruby, tips
- Homepage:
- Size: 1.95 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ruby Pro Tips
### Use `Time#getutc` instead of `Time#utc`
```rb
# ❌
now = Time.nownow # 2024-03-15 09:42:43.57382 +0100
now.utc # 2024-03-15 08:42:43.57382 UTC
now # 2024-03-15 08:42:43.57382 UTC# ✅
now = Time.now
now # 2024-03-15 09:42:43.57382 +0100
now.getutc # 2024-03-15 08:42:43.57382 UTC
now # 2024-03-15 09:42:43.57382 +0100
```###
```rb
class User < ApplicationRecord
has_many :orders
has_many :reviews# ❌
scope :recent_orders, -> {
joins(:orders).where(orders: { created_at: 30.days.ago.. })
}scope :top_rated, -> {
joins(:reviews).where(reviews: { rating: 4.. })
}
# This fails! Returns 4 instead of 1
# Because: 2 orders x 2 reviews = 4 records
User.bad_recent_orders.bad_top_rated.count # => 4# ✅
scope :recent_orders, -> {
where(id: Order.recent_orders.select(:user_id))
}
scope :top_rated, -> {
where(id: Review.top_rated.select(:user_id))
}
# This works! Will not miscount!
# PLUS scopes are DRY-ly defined where they belong!
# PLUS multiple models can have the same scope behavior!
User.good_recent_orders.good_top_rated.count # => 1
end
```###
```rb
class Account < ApplicationRecord
normalizes :billing_email, with: -> { it.presence }
end
```