Skip to content

Commit

Permalink
Merge pull request #10 from upfluence/ej/update-readme-with-scope
Browse files Browse the repository at this point in the history
Document scope feature
  • Loading branch information
lordteka authored Feb 21, 2024
2 parents 692f8fc + a53dd5c commit 5352143
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 matches some criterias.
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 5352143

Please sign in to comment.