#1472 unconfirmed
Bernerd Schaefer

Union across associations resets foreign keys

Reported by Bernerd Schaefer | January 10th, 2011 @ 11:15 AM

Given the following models:

    class Activity
      belongs_to :post, required: false
      belongs_to :user, required: false
    end

    class Post
      belongs_to :user
      has n, :activities
    end

    class User
      has n, :posts
      has n, :activities

      def all_activities
        activities | posts.activities
      end
    end

When User#all_activities is called, the foreign keys of post activities are re-bound to the User model:

    user = User.create
    user.activities.create

    post = Post.create(user: user)
    post.activities.create

    user_activity, post_activity = user.all_activities

    assert_equal user.id, user_activity.user_id
    assert_equal nil, user_activity.post_id

    assert_equal post.id, post_activity.post_id
    assert_equal nil, post_activity.user_id
    # FAIL:
    #   <nil> expected but was <1>
    #   post_activity.dirty_attributes # => {:user_id => 1}

Full test case here: https://gist.github.com/773035

No comments found

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Pages