I see a strange error since I switched from Rails 3.0.11 to 3.1.3. Here is a separate code for reproducing the error:
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'mysql2',
:username => 'root',
:database => "some_development"
)
class User < ActiveRecord::Base
has_many :favorites
end
class Favorite < ActiveRecord::Base
belongs_to :user
end
u = User.create
f = u.favorites.find_or_initialize_by_site_id(123)
f.some_attr = 'foo'
f.save!
u.name = 'bar'
u.save!
finishes ActiveRecord::RecordNotUniquetrying INSERTthe same entry in the table favorites. (Note that in this example, the pair (user_id, site_id) must be unique in favorites)
Interestingly, if I use find_or_createinstead find_or_initialize, there will be no exceptions.
In the stack trace, I noticed that I was autosave_associationreceiving a call, I donβt know why, but in fact it also removes the error has_many :favorites, :autosave => falseinstead has_many :favorites. Since I never cared about autosave, Iβm not even sure that :autosave => falseis a good idea or not.
, Rails? - ?