Pregunta

I have a "rota" model with an attribute "turn_index". For some reason, update_attributes does not seem to be working. Any clue why?

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

The schema for rota is:

  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

Rota Model:

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
¿Fue útil?

Solución

On before_save, you're setting turn_index to 0. You can fix this by only setting it on create:

before_save :set_turn_index, on: :create

Or set the default value on turn_index to 0 in your migration.

Otros consejos

Your before_save is always setting turn_index to 0

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top