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

#find through non-hashid parent association does not work #61

Open
jcypret opened this issue May 25, 2019 · 5 comments
Open

#find through non-hashid parent association does not work #61

jcypret opened this issue May 25, 2019 · 5 comments
Assignees
Labels

Comments

@jcypret
Copy link
Owner

jcypret commented May 25, 2019

If you have a has_many association where the parent does not use hashids, find through the association doesn't return the associated record. I added a failing test case to demonstrate.

It seems that to solve this, apart from overriding .find on the model class, we would have to override ActiveRecord::Associations::CollectionProxy's #find method.

Originally posted by @ahawrylak in #55 (comment)

@jcypret jcypret added the bug label May 25, 2019
@jcypret jcypret self-assigned this May 26, 2019
@geekdeepak
Copy link

this is a real issue for me too.

@jacobperia
Copy link

can this not be fixed? it's a big issue :(

@jtoy
Copy link

jtoy commented Nov 22, 2022

I just ran into this as well. What should the work around be for this?

@airblade
Copy link

@jtoy A workaround is to use find_by_hashid. For example:

post.comments.find_by_hashid(comment.hashid)

But it's much clunkier.

@airblade
Copy link

Another workaround is to extend the association explicitly:

class Post
  has_many :comments do
    def find(*)
      find_by_hashid(*)
    end
  end
end

I have been trying to figure out how to extend the association automatically. Starting from the model with the hashid (Comment, in this case) you can get its belong-to associations via Comment.reflect_on_all_associations(:belongs_to), and get their inverses (if you have specified the inverse on the belongs_to associations) via .map(&:inverse_of), which are the has-many reflections (e.g. Post's has-many reflection for its comments association). But I don't know how to get from there to the association proxy (post.comments) in order to include/extend it with the behaviour we want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants