Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...