Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Aug 15, 2024
1 parent 6ffbe5f commit bcb727f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,33 @@ the association is nil, the null object will be returned instead.
class NullPlan
include Singleton

def name = 'Free'
def free? = true
def pro? = false
def ent? = false
end
```

```ruby
class Account
belongs_to :plan, optional: true, null_object: NullPlan.instance
end
```

```ruby
account = Account.create

puts account.plan # => #<NullPlan name="Free">
puts account.plan.free? # => true
puts account.plan.ent? # => false

account.update(plan: Plan.new(name: 'Ent', ent: true))

puts account.plan # => #<Plan id=1 name="Ent">
puts account.plan.free? # => false
puts account.plan.ent? # => true
```

## Supported Rubies

**`null_association` supports Ruby 3.1 and above.** We encourage you to upgrade
Expand Down

0 comments on commit bcb727f

Please sign in to comment.