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 ?

titibouboul
  • 1,348
  • 3
  • 16
  • 30

1 Answers1

95

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.

Dennis
  • 56,821
  • 26
  • 143
  • 139
DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55
  • 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? – moosefetcher Sep 18 '14 at 14:01
  • 5
    Yes the last line is doing that. It is automatically serialized and de serialized under the hood. – DiegoSalazar Sep 18 '14 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'. – moosefetcher Sep 18 '14 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? – moosefetcher Sep 18 '14 at 14:31
  • There's a number of ways to (de)serialize manually. Look into YAML.dump, Marshall.dump, and JSON. For a more thorough answer I suggest posting a question than to so this in the comments. I'll be glad to elaborate if you link me to your question. – DiegoSalazar Sep 18 '14 at 14:31
  • Is Comment a model? Those go in the models folder. – DiegoSalazar Sep 18 '14 at 14:32
  • I've asked a question here... http://stackoverflow.com/questions/25916449/saving-and-retrieving-an-array-in-rails – moosefetcher Sep 18 '14 at 15:14
  • I'm guessing the `stuff` field would be of type string when stubbing out the model? – Dennis Oct 02 '14 at 13:55
  • 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 02 '14 at 14:33