开发者

rails store array in to database field

开发者 https://www.devze.com 2023-03-05 11:52 出处:网络
I want to store an array into a database field. I tried the following method: class MyStuff < ActiveRecord::Base

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"
..
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号