Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save array to database in rails

if i have params like this :

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

and when i create object to database field state_id not save to database?

how to save to database with format :

#<Scholarship id: 1, name: "test", state_id: "["1", "2", "3", "4"]"> 

how do that?

thanks before

like image 418
tardjo Avatar asked Apr 28 '14 11:04

tardjo


1 Answers

Also you can use PostrgreSQL support for array storing. (If you're using PG of course). Your migration will look like that:

add_column :table_name, :column_name, :string, array: true, default: [] 

But don't forget about validations.

like image 161
cnnr Avatar answered Sep 25 '22 10:09

cnnr