简体   繁体   English

Rails ActiveRecord和原始类型数组

[英]Rails ActiveRecord and array of primitive types

What is the best way to store array of of primitive types using Rails activerecord? 使用Rails activerecord存储基本类型数组的最佳方法是什么?

For example I've got article model, which has images property. 例如,我有文章模型,它有图像属性。 Images property is array of image urls. Images属性是图像URL的数组。

I don't wont use separate table to store this array. 我不会使用单独的表来存储此数组。

Regards, Alexey Zakharov 此致,Alexey Zakharov

You can use ActiveRecord::Base.serialize . 您可以使用ActiveRecord :: Base.serialize It will save the object as YAML in database. 它将对象保存为数据库中的YAML。 You need to first create the column with :text or :string as its type. 您需要首先使用:text:string作为其类型创建列。

class Article
  serialize :image_urls
end

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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