Skip to content

Commit a53dd5c

Browse files
committed
document scope feature
1 parent 692f8fc commit a53dd5c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,31 @@ Way nicer, right?
105105
Just like AMS, USerializer supports `has_one` and `has_many`
106106
relationships
107107

108+
### Collection Attributes Filtering
109+
110+
For `has_many` relationships, USerializer allow you to serialize only
111+
part of the collection that matches some criterias.
112+
It relies on the ActiveRecord `scope` feature :
113+
114+
```ruby
115+
class Product < ActiveRecord::Base
116+
has_many :variants
117+
end
118+
119+
class Variant < ActiveRecord::Base
120+
belongs_to :product
121+
122+
scope :available, -> { where(delete_at: nil) }
123+
end
124+
125+
class ProductSerializer < USerializer::BaseSerializer
126+
has_many :variants, scope: :available
127+
end
128+
129+
class VariantSerializer < USerializer::BaseSerializer
130+
end
131+
```
132+
108133
### Serialized Output
109134

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

0 commit comments

Comments
 (0)