Pier-Olivier Thibault
2012-Jun-21 14:22 UTC
ActiveModel::Serializers::JSON support arbitrary keys
Is there a reason why AM:S doesn''t support arbitrary keys to serialization? I can see a lot of uses cases where arbitrary keys could come handy. Right now, if one tries to do it, AM:S checks if the keys match a method of the model and raises exception if it can''t find any. class Client < ActiveRecordBase attr_accessible :name, :address, as: :creator include ActiveModel::Serializers::JSON def attributes {label: name, value: id} end While I can easily create a method for all the keys I want to create, take the example above, where a jquery plugin requires a data structure of "value: id, label: string". Does it really make sense to create a value and label method in a model? I can see this becoming cumbersome real fast (different values for different query). -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/td2Y13_PRhIJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Carlos Antonio da Silva
2012-Jun-21 15:42 UTC
Re: ActiveModel::Serializers::JSON support arbitrary keys
Just linking the related discussion on issues tracker: https://github.com/rails/rails/issues/6811 -- At. Carlos Antonio On Thursday, June 21, 2012 at 11:22 AM, Pier-Olivier Thibault wrote:> > Is there a reason why AM:S doesn''t support arbitrary keys to serialization? I can see a lot of uses cases where arbitrary keys could come handy. Right now, if one tries to do it, AM:S checks if the keys match a method of the model and raises exception if it can''t find any. > > class Client < ActiveRecordBase attr_accessible :name, :address, as: :creator include ActiveModel::Serializers::JSON def attributes {label: name, value: id} end > > While I can easily create a method for all the keys I want to create, take the example above, where a jquery plugin requires a data structure of "value: id, label: string". Does it really make sense to create a value and label method in a model? I can see this becoming cumbersome real fast (different values for different query). > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/td2Y13_PRhIJ. > To post to this group, send email to rubyonrails-core@googlegroups.com (mailto:rubyonrails-core@googlegroups.com). > To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com (mailto:rubyonrails-core+unsubscribe@googlegroups.com). > For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Possibly Parallel Threads
- [PATCH] add :root option to ActiveModel JSON serialization
- Key Value Json ActiveModel::Errors
- how to use activemodel collection.build for a has_many :through association
- NoMethodError (undefined method `serializable_record' for #<ActiveModel
- active_model_serializers, more than one level deep of associations, specifying serializers per association