Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise ArgumentError when has_one is used with counter_cache #13107

Merged
merged 1 commit into from
Nov 30, 2013

Conversation

chancancode
Copy link
Member

I came across this when working on an app that has something like this:

class University < AR::Base
  has_many :researchers
  has_many :publications, through: :researchers
end

class Researchers < AR::Base
  belongs_to :university
  has_many :publications
end

class Publication < AR::Base
  belongs_to :researcher
  has_one :university, through: :researchers, counter_cache: true
end

Of course it wasn't working, and that I later realized that this is not supposed to work at all, and I was wondering why it didn't raise an error. After some digging, it seems like this is a bug that was introduced in 52f8e4b.

cc @senny, @jonleighton

Previously, the `has_one` macro incorrectly accepts the `counter_cache` option
due to a bug, although that options was never supported nor functional on
`has_one` and `has_one ... through` relationships. It now correctly raises an
`ArgumentError` when passed that option.

For reference, this bug was introduced in 52f8e4b.
senny added a commit that referenced this pull request Nov 30, 2013
…ld_raise

Raise `ArgumentError` when `has_one` is used with `counter_cache`
@senny senny merged commit 61ad8d5 into rails:master Nov 30, 2013
@senny
Copy link
Member

senny commented Nov 30, 2013

@chancancode thank you 💛

senny added a commit that referenced this pull request Nov 30, 2013
…ld_raise

Raise `ArgumentError` when `has_one` is used with `counter_cache`
Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/associations/builder/belongs_to.rb
	activerecord/lib/active_record/associations/builder/singular_association.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants