how to save array to database in rails

前端 未结 4 856
我在风中等你
我在风中等你 2020-12-10 01:07

if i have params like this :

params[\"scholarship\"] = {\"name\"=>\"test\", \"state_ids\"=>[\"1\", \"2\", \"3\", \"4\"]}

and when i c

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 01:27

    ActiveRecord::Base.serialize.

    For example:

    class User < ActiveRecord::Base
      serialize :scholarship
    end
    
    user = User.create(:scholarship=> { "name" => "test", "state_ids" => ["1", "2"]})
    User.find(user.id).scholarship# => { "name" => "test", "state_ids" => ["1", "2"] }
    

提交回复
热议问题