简体   繁体   中英

Rails ActiveRecord and array of primitive types

What is the best way to store array of of primitive types using Rails activerecord?

For example I've got article model, which has images property. Images property is array of image urls.

I don't wont use separate table to store this array.

Regards, Alexey Zakharov

You can use ActiveRecord::Base.serialize . It will save the object as YAML in database. You need to first create the column with :text or :string as its type.

class Article
  serialize :image_urls
end

article.image_urls = ['/images/image1.png', '/images/image2.png']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM