49

With ruby-on-rails, I want to store an array of 3 elements: the last 3 comments of a post. I know I could join the Comment table to the Post one, but I would avoid to do this heavy request for scaling purposes.

So I was wondering what was the best way to store those 3 elements, as I would like to update them easily every time a new comment is made: remove the last comment and add the new one.

What is the correct way to do this ? Store it in a serialized array or in a JSON object ?

5
  • 4
    If you're on postgres, it has array column type. Jan 23, 2014 at 15:21
  • I'm using postgres : you recommend using their array column type ? Jan 23, 2014 at 15:22
  • 3
    Obviously. Why would you want to mess with JSON, if you have arrays. Jan 23, 2014 at 15:23
  • I'm using Rails 3.2 and it doesn't seem to support that function of PG Jan 23, 2014 at 15:27
  • 1
    @titibouboul It is a good practice to add your stack details especially rails and ruby version in question by default. It saves a lot of your time and experienced people can answer better and faster (:
    – ARK
    Dec 3, 2019 at 13:22

1 Answer 1

96

You can store Arrays and Hashes using ActiveRecord's serialize declaration:

class Comment < ActiveRecord::Base
  serialize :stuff
end

comment = Comment.new  # stuff: nil
comment.stuff = ['some', 'stuff', 'as array']
comment.save
comment.stuff # => ['some', 'stuff', 'as array']

You can specify the class name that the object type should equal to (in this case Array). This is more explicit and a bit safer. You also won't have to create the array when you assign the first value, since you'll be able to append to the existing (empty) array.

class Comment < ActiveRecord::Base
  serialize :stuff, Array
end

comment = Comment.new  # stuff: []
comment.stuff << 'some' << 'stuff' << 'as array'

You can even use a neater version called store: http://api.rubyonrails.org/classes/ActiveRecord/Store.html

This should handle your use case using a built in method.

9
  • Once an Array is serialized, how do you extract it from the database? I THINK the last line of this answer might be referring to that, but I'm not sure. If there's a built-in method for de-serializing an Array, what is it? Sep 18, 2014 at 14:01
  • 5
    Yes the last line is doing that. It is automatically serialized and de serialized under the hood. Sep 18, 2014 at 14:03
  • Thanks for the reply. As a Rails noobie, I'm asking you to spell out how that's done 'above the hood'. Sep 18, 2014 at 14:08
  • OK, I think I see what you mean. The last line of CODE does the de serializing. I hope you don't mind, but NOW I'm wondering where I would put the Comment class for a Rails project. Does it belong in the controllers folder? Sep 18, 2014 at 14:31
  • 4
    To answer my own question, a type of string or text would work for this example, though text should be preferred since you don't want to accidentally run into the max length limit.
    – Dennis
    Oct 2, 2014 at 14:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.