Skip to content

Commit

Permalink
document scope feature
Browse files Browse the repository at this point in the history
  • Loading branch information
lordteka committed Feb 21, 2024
1 parent 692f8fc commit 1eb9f7a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ Way nicer, right?
Just like AMS, USerializer supports `has_one` and `has_many`
relationships

### Collection Attributes Filtering

For `has_many` relationships, USerializer allow you to serialize only
part of the collection that match some criteria.
It relies on the ActiveRecord `scope` feature :

```ruby
class Product < ActiveRecord::Base
has_many :variants
end

class Variant < ActiveRecord::Base
belongs_to :product

scope :available, -> { where(delete_at: nil) }
end

class ProductSerializer < USerializer::BaseSerializer
has_many :variants, scope: :available
end

class VariantSerializer < USerializer::BaseSerializer
end
```

### Serialized Output

The following outputs will be based an on our `Order` object in
Expand Down

0 comments on commit 1eb9f7a

Please sign in to comment.