旧代码,在 Rails 3.0 中工作:
belongs_to :primary_stream
before_save :autocreate_primary_stream, :if=>lambda {|a| a.primary_stream.nil?}
def autocreate_primary_stream
self.create_primary_stream()
end
在 Rails 3.1 中:
self.primary_stream
已填充,并且self.primary_stream_id
为 nil。保存记录时,primary_stream_id 以 nil 形式保存到数据库中。
我不得不这样做,以获得我期望的行为:
belongs_to :primary_stream
before_save :autocreate_primary_stream, :if=>lambda {|a| a.primary_stream.nil?}
def autocreate_primary_stream
self.create_primary_stream()
self.primary_stream_id = primary_stream.id
end
有什么改变,还是我做了一些非常愚蠢的事情?