8

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

1 Answer 1

11

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']
Sign up to request clarification or add additional context in comments.

1 Comment

Great! I've dreamed about such feature when using asp.net mvc :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.