Rails: простые update_attributes не работают
У меня есть модель "rota" с атрибутом "turn_index". По некоторым причинам update_attributes, похоже, не работает. Любая подсказка, почему?
rota = Rota.create
rota.turn_index.should == 0 -- passes
rota.update_attributes(:turn_index=>1)
rota.turn_index.should == 1 -- fails
Схема для вращения:
create_table "rotas", :force => true do |t|
t.string "name"
t.integer "turn_index"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
Рота Модель:
class Rota < ActiveRecord::Base
has_many :rotazations
has_many :users, :through => :rotazations
has_many :invitations
before_save :set_turn_index
private
def set_turn_index
self.turn_index = 0
end
end
2 ответа
Решение
На before_save
Вы устанавливаете turn_index
в 0. Вы можете исправить это, только установив его при создании:
before_save :set_turn_index, on: :create
Или установите значение по умолчанию на turn_index
до 0 в вашей миграции.