File tree 1 file changed +25
-0
lines changed
1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -105,6 +105,31 @@ Way nicer, right?
105
105
Just like AMS, USerializer supports ` has_one ` and ` has_many `
106
106
relationships
107
107
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
+
108
133
### Serialized Output
109
134
110
135
The following outputs will be based an on our ` Order ` object in
You can’t perform that action at this time.
0 commit comments