mysql - Best way to declare a rails array field? -


what best way/practice declare rails(4) array field (with mysql database)? need store ids array. tried using activerecord serializer , customized attribute accessors field can behave array.

class officeids < activerecord::base   serialize :office_ids    def office_ids=(ids)    ids = ids.join(",") if ids.is_a?(array)    write_attribute(:office_ids, ids)   end    def office_ids    (read_attribute(:office_ids) || "").split(",")   end  end 

i feel not best approach kind of situation. appreciated. thanks!

if you're using serializer, there's no need write wrapper method this. should able assign arbitrary objects field:

ids = officeids.new ids.office_ids = [ 1, 2, 3 ] ids.save 

it rather odd have model called officeids though, plural name willc cause kinds of trouble. sure don't want traditional has_many relationship these?


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

django - CSRF verification failed. Request aborted. CSRF cookie not set -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -