Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

2.1.0 has_many associations broken

#447

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:

  1. self["#{reflection.primary_key_name}"] = association.id

Reported by Pavel Golubeff · June 18th, 2008 @ 07:41 PM

State: invalid
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    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.

    June 21st, 2008 @ 06:51 PM

  2. Pavel Golubeff
  3. Pavel Golubeff
    Pavel Golubeff

    There's a patch, i've made.

    June 20th, 2008 @ 03:06 PM

  4. Pavel Golubeff
    Pavel Golubeff
    • No changes were found…

    June 20th, 2008 @ 03:13 PM

  5. Pavel Golubeff
    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.

    June 19th, 2008 @ 10:03 AM

  6. Pavel Golubeff
    Pavel Golubeff

    I've got an addition. The problem occurs only in test environment.

    June 19th, 2008 @ 09:13 AM

  7. Pavel Golubeff
    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:

    1. self["#{reflection.primary_key_name}"] = association.id

    June 18th, 2008 @ 07:41 PM

  8. Prem Sichanugrist (sikachu)
    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

    January 22nd, 2010 @ 08:53 AM

  9. Pratik
    Pratik
    • State changed from stale to invalid

    January 22nd, 2010 @ 10:28 AM