I want to store an array into a database field. I tried the following method:
class MyStuff < ActiveRecord::Base
serialize :t开发者_运维知识库hings
end
stuff = MyStuff.new
stuff.things << "pen"
stuff.things << "paper"
stuff.save
I get an error: "The error occurred while evaluating nil.<<"
Is there any other approach?
What is "things". Define it to be Array or Hash or something you want that to be and then add elements to it.
stuff = MyStuff.new
stuff.things = []
stuff.things << "pen"
..
stuff.save
you can use this, this will work for existing things as well.
stuff.things ||= [] //without || your existing things will be reset
stuff.things << "pen"
..
精彩评论