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

Instance dependent association preloading which was removed has valid use cases #23435

Closed
agios opened this issue Feb 2, 2016 · 2 comments
Closed

Comments

@agios
Copy link

agios commented Feb 2, 2016

4ed9797 removed support for preloading instance-dependent associations.

While it is understandable that they might have been a source of confusion, associations such as the one presented in #15024 which this addressed, can be properly preloaded in this manner:

class User < ActiveRecord::Base
  has_many :posts, ->(sender) {
    title_condition =
      if sender.is_a? User
        sender.name
      else # Relation
        User.arel_table[:name]
      end
    where(arel_table[:title].eq(title_condition))
  }
end

This code results in an association that can both be called on an instance as well as preloaded in another query. Removing this feature actually breaks working code.

What do you think @senny, @rafaelfranca?

@rafaelfranca
Copy link
Member

It was already removed and we don't plan to add it back. Even that you can make it work it still have to check if you are in a instance. Also to make it work you are relying in private API of the framework arel_table.

@rafaelfranca
Copy link
Member

This is a dup of #22560

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

No branches or pull requests

2 participants