Skip to content

Commit

Permalink
Add a note for the second parameter of if to README
Browse files Browse the repository at this point in the history
  • Loading branch information
okuramasafumi committed Jul 14, 2024
1 parent b3b5f57 commit 1208998
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,36 @@ user = User.new(1, nil, nil)
UserResource.new(user).serialize # => '{"id":1}'
```
#### Caution for the second parameter in `if` proc
`if` proc takes two parameters. The first one is the target object, `user` in the example above. The second one is `attribute` representing each attribute `if` option affects. Note that it actually calls attribute methods, so you cannot use it to prevent attribute methods called. This means if the target object is an `ActiveRecord::Base` object and using `association` with `if` option, you might want to skip the second parameter so that the SQL query won't be issued.
Example:
```ruby
class User < ApplicationRecord
has_many :posts
end

class Post < ApplicationRecord
belongs_to :user
end

class UserResource
include Alba::Resource

# Since `_posts` parameter exists, `user.posts` are loaded
many :posts, if: proc { |user, _posts| user.admin? }
end

class UserResource2
include Alba::Resource

# Since `_posts` parameter doesn't exist, `user.posts` are NOT loaded
many :posts, if: proc { |user| user.admin? }
end
```
### Default
Alba doesn't support default value for attributes, but it's easy to set a default value.
Expand Down

0 comments on commit 1208998

Please sign in to comment.