This project is archived and is in readonly mode.
2.1.0 has_many associations broken
-
Pratik
- State changed from new to incomplete
- Assigned user set to Pratik
I don't like the solution to reload. Why not just set the instance var where AR updates the foreign key ?
Thanks.
-
Pavel Golubeff
There's a patch, i've made.
-
Pavel Golubeff
- No changes were found…
-
Pavel Golubeff
I attached a simple example illustrating the problem. There are two models Post and Comment, and a test case, prooving a bug.
Actually the problem occurs, because << does not call reload on associated object and does not get changed owner. So, in my perception, if << reload object, everything would be fine.
-
Pavel Golubeff
I've got an addition. The problem occurs only in test environment.
-
Pavel Golubeff
Hi there.
After installing 2.1.0 rails gem i experience problem with has_many associations.
It's impossible to change object owner, if it has one already.
For example:
user1 = User.create
>> #<User id: 1>
user2 = User.create
>> #<User id: 2>
char = Character.create( :user => user1 )
>> #<Character id:1, :user_id => 1>
user2.characters << char
char.user_id
>> 1
As far as i'm concirned, breaking code is situated in active_record/associations.rb, line 898, where belongs_to_before_save_for_#{reflection.name} is defined.
association is taken from char.instance_variable_get("@user") and in case it is defined, it executes char[:user_id] = @user.id.
Commenting this line make everyone happy, but i'm not sure, if it's a good idea:
- self["#{reflection.primary_key_name}"] = association.id
-
Prem Sichanugrist (sikachu)
- State changed from new to stale
- Tag set to 2.1, activerecord, bug, edge, has_many, patch, tested
- Assigned user set to Pratik
-
Pratik
- State changed from stale to invalid
