Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 221
Alexey Zakharov Avatar asked Sep 17 '10 04:09

Alexey Zakharov


Video Answer


1 Answers

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']
like image 154
htanata Avatar answered Sep 17 '22 12:09

htanata