This project is archived and is in readonly mode.

#5952 ✓stale
Chowlett

:inverse_of does not apply to association extensions

Reported by Chowlett | November 11th, 2010 @ 12:32 PM

(Note: this is almost a direct crosspost of this StackOverflow question.)

If you create a :has_many relationship with :inverse_of, and define an extension on the association to return a subset of the associated objects, then objects returned by the extension method don't benefit from the :inverse_of declaration.

For example:

class Player < ActiveRecord::Base
  has_many :cards, :inverse_of => :player do
    def in_hand
      find_all_by_location('hand')
    end
  end
end

class Card < ActiveRecord::Base
  belongs_to :player, :inverse_of => :cards
end

# Player#cards benefits from :inverse_of as follows:
p = Player.find(:first)
c = p.cards[0]
p.score # => 2
c.player.score # => 2
p.score += 1
c.player.score # => 3
c.player.score += 2
p.score # => 5

# ... but Player#cards.in_hand doesn't:
p = Player.find(:first)
c = p.cards.in_hand[0]
p.score # => 2
c.player.score # => 2
p.score += 1
c.player.score # => 2
c.player.score += 2
p.score # => 3

d = p.cards.in_hand[1]
d.player.score # => 2

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh November 12th, 2010 @ 11:57 AM

    • Importance changed from “” to “Low”

    Which version of rails you are using?

  • Chowlett

    Chowlett November 12th, 2010 @ 12:25 PM

    >rails --version
    Rails 3.0.0

    (This is my first Lighthouse ticket, so if I've missed out useful information please do ask for it!)

  • Santiago Pastorino

    Santiago Pastorino February 12th, 2011 @ 07:18 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 12th, 2011 @ 07:18 PM

    • State changed from “open” to “stale”

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages