我想在ActiveRecord模型(pos)中分配一个字段到目前为止的最高值1+。以下任一版本都会产生无限循环。我无法弄清楚为什么。你能看见它吗?这为什么会产生一个无限循环?
class Question < ActiveRecord::Base
attr_accessible :text, :pos, :data_type, :active
has_many :values
belongs_to :program
after_initialize :assign_pos
def row_label
text
end
def self.highest_pos
self.order("pos DESC").first
end
def assign_pos
puts "********* #{Question.highest_pos + 1}" # option 1
self.pos = Question.highest_pos + 1 # option 2
end
end
这并不是做到这一点。让数据库增加值。唯一的缺点是,在记录保存之前,您不会提前知道新值。 – 2012-11-20 21:43:28
此外,它不是一个增量,因为用户之间可能已经改变了pos值 – pitosalas