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

Has-many-through fails to create join model when used with scope #19073

Closed
RKushnir opened this issue Feb 25, 2015 · 2 comments
Closed

Has-many-through fails to create join model when used with scope #19073

RKushnir opened this issue Feb 25, 2015 · 2 comments
Assignees
Milestone

Comments

@RKushnir
Copy link
Contributor

I have the following models

class User < ActiveRecord::Base
  has_many :likes, dependent: :destroy
  has_many :liked_collections, through: :likes, source: :liked, source_type: 'Collection'
end

class Like < ActiveRecord::Base
  belongs_to :user
  belongs_to :liked, polymorphic: true, counter_cache: true
end

class Collection < ActiveRecord::Base
  scope :with_public_access, -> { where(private: false) }
end

Now, user.liked_collections.create! works.
And user.liked_collections.with_public_access.create! creates the collection, but fails to create a like to link it to the user.

UPD. Gist to reproduce https://gist.github.com/RKushnir/57d0064541da39f9b8e4

@rafaelfranca
Copy link
Member

Could you create an reproduction script with this template https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_master.rb?

@RKushnir
Copy link
Contributor Author

@rafaelfranca of course, here you are https://gist.github.com/RKushnir/57d0064541da39f9b8e4
I simplified it a bit and removed the details that appeared to be irrelevant.

@RKushnir RKushnir changed the title Has-many-through-polymorphic fails to create join model when used with scope Has-many-through fails to create join model when used with scope Feb 25, 2015
@sgrif sgrif self-assigned this Feb 25, 2015
@sgrif sgrif added this to the 4.2.2 milestone Feb 25, 2015
@sgrif sgrif closed this as completed in 3821892 Feb 26, 2015
sgrif added a commit that referenced this issue Feb 26, 2015
Various behaviors needed by associations (such as creating the through
record) are lost when `where` is called, since we stop having a
`CollectionProxy` and start having an `AssociationRelation` which does
not contain this behavior. I *think* we should be able to rm
`AssociationRelation`, but we have tests saying the changes required to
do that would be bad (Without saying why. Of course. >_>)

Fixes #19073.

Conflicts:
	activerecord/CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants