Rails:简单的update_attributes不起作用

时间:2012-05-05 15:15:15

标签: ruby-on-rails attributes update-attributes

我有一个带有“turn_index”属性的“rota”模型。出于某种原因,update_attributes似乎不起作用。有什么线索的原因?

  rota = Rota.create
  rota.turn_index.should == 0 -- passes
  rota.update_attributes(:turn_index=>1)
  rota.turn_index.should == 1 -- fails

rota的架构是:

  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 个答案:

答案 0 :(得分:0)

您的before_save始终将turn_index设为0

答案 1 :(得分:0)

before_save上,您将turn_index设置为0.您只需在创建时设置即可解决此问题:

before_save :set_turn_index, on: :create

或者在迁移中将turn_index上的默认值设置为0.