Rails: display of array as string in rails -
i have model field array. can store , retrieve database no trouble.
when user creates or edits array want them see , edit comma separated list. there sensible reasons behind choice store array , have user interact list, plus edit records containing list/array. 99% of time edit record once, when create it, although may view many times, view times record not in edit mode not concerned performance hit coming using serialize.
i know how split string array of strings, , know how join , array of strings make comma separated list.
my dilemma revolves around split/join? in controller private methods called before display/before save, in model, or maybe form helper? i've no idea.
advice appreciated.
you don't need convert text. activerecord::base class macro serialize
built such use case - saving arrays, hashes, , other non-mappable objects in text columns
active record can serialize object in text columns using yaml. so, must specify call class method serialize. makes possible store arrays, hashes, , other non-mappable objects without doing additional work.
class user < activerecord::base serialize :preferences end user = user.create(preferences: { "background" => "black", "display" => large }) user.find(user.id).preferences # => { "background" => "black", "display" => large }
doc: http://api.rubyonrails.org/classes/activerecord/base.html see serialize part
Comments
Post a Comment