hi please gice explanation for this
1: def json_attributes_for(model, *attrs)
2: attrs = [attrs].flatten.map(&:to_s)
3: model.attributes.delete_if{|k,v| !attrs.include?(k) }.to_json
4: end
hi please gice explanation for this
1: def json_attributes_for(model, *attrs)
2: attrs = [attrs].flatten.map(&:to_s)
3: model.attributes.delete_if{|k,v| !attrs.include?(k) }.to_json
4: end
On Jul 18, 6:08 am, Lost W. [email protected]
wrote:
hi please gice explanation for this
1: def json_attributes_for(model, *attrs)
2: attrs = [attrs].flatten.map(&:to_s)
3: model.attributes.delete_if{|k,v| !attrs.include?(k) }.to_json
4: end
It returns a json hash for the object, but only including the
specified attributes. It’s rewriting the wheel though, first of hashes
have a slice method so that messing around with delete_if is not
needed, and secondly to_json takes a :only option.
Fred
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs