无法将数组值存储到rails 2.x中的单个db表列?

时间:2015-12-11 06:37:29

标签: mysql ruby-on-rails ruby ruby-on-rails-2

Html代码是:

Utilities: <br>
<input type="checkbox" name="amenity[utilities][]" value="Gas">Gas<br>
<input type="checkbox" name="amenity[utilities][]" value="Electricity">Electricity<br>
<input type="checkbox" name="amenity[utilities][]" value="Water">Water<br>
<input type="checkbox" name="amenity[utilities][]" value="Parking">Parking<br>

上面的html将数据存储在DB Table列中,如下所示:

--- 
- Gas
- Electricity
- Parking
- Water

enter image description here

1 个答案:

答案 0 :(得分:1)

这是因为Rails将对象序列化为YAML以便将其存储在数据库中。你看到的实际上是一个YAML序列化数组。这里:

require 'yaml'
dbstr = "--- 
- Gas
- Electricity
- Parking
- Water"
# => "--- \n- Gas\n- Electricity\n- Parking\n- Water"
YAML::load dbstr
# => ["Gas", "Electricity", "Parking", "Water"]

另请查看ActiveRecord::Base#serialize

的文档