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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actables function does not work with :as #46

Open
Taeir opened this issue Aug 5, 2021 · 0 comments
Open

Actables function does not work with :as #46

Taeir opened this issue Aug 5, 2021 · 0 comments

Comments

@Taeir
Copy link
Contributor

Taeir commented Aug 5, 2021

The relation method actables (ActsAs::ReflectionsWithActsAs) is not documented in the README (I was about to propose a feature request for this functionality 馃槄, I've added a small PR to add it in there).

That aside, actables seems to only work when the default name of actable is used (it uses hardcoded where(actable_id: ...)).

So a configuration like

class Product < ActiveRecord::Base
  actable as: :producible
end

class Pen < ActiveRecord::Base
  acts_as :product, as: :producible
end

Product.where(price: 0.8).actables

will not work.

I believe to make actables work with differently named relations, the ActsAs::Relations::ClassMethods needs to define a way to retrieve the as parameter, or pass it along somehow. It would also need to define a method on the class instance with the plural of what is passed to the as. Something along the lines of:

...

singleton_class.module_eval do
  include ActsAs::ClassMethods
  
  def <as.to_s.pluralize.to_sym>
    acting_as_model.where("#{as}_id" => select(:id))
  end
end

I'm afraid I do not quite know how to do that, or I would suggest a pull request myself. However, perhaps it is possible for you to add this?

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

1 participant