我正在运行Rails 4.2.3。我有一个模型Volunteer
,带有序列化属性participated
。创建对象后,我无法更新participated
属性:
> @volunteer = Volunteer.new( participated: {'citizenship' => '', 'daca' => ''} )
=> #<Volunteer id: 1, participated: {'citizenship' => '', 'daca' => ''}>
> @volunteer.participated['citizenship'] = 'test'
=> {'citizenship' => 'test', 'daca' => ''}
> @volunteer
=> #<Volunteer id: 1, participated: {'citizenship' => 'test', 'daca' => ''}>
> @volunteer.changed?
=> true
> @volunteer.save
=> true
> Volunteer.last
=> #<Volunteer id: 1, participated: {'citizenship' => '', 'daca' => ''}>
如您所见,数据库未更新。我原来是运行rails 4.2.0。那时,changed?
甚至没有返回true
- 在这种情况下,它正在返回false
。经过一番搜索,GitHub上的this pull request让我怀疑这个问题可能是一个Rails错误。更新到rails 4.2.3修复了changed?
的问题,但数据库仍未更新。
任何建议都将不胜感激!到目前为止,我已经陷入了几个小时。
它的价值:
class Volunteer < ActiveRecord::Base
serialize :participated
end
class CreateVolunteers < ActiveRecord::Migration
def change
create_table :volunteers do |t|
t.text :participated
t.timestamps null: false
end
end
end