Open uberllama opened 11 years ago
I tried doing the same thing with my Comment model and didn't encounter any issues, which leads me to believe there's a reserved term issue with models named "Document" and pg_search.
Finally had time to open up the lib and there it is, Document as an un-namespaced class. Would there be any chance of either namespacing this under PgSearch or renaming it to something like SearchDocument?
I am pretty sure that the included AR document class is already name spaced as PgSearch::Document.
Do you get the same error if you try to access your own Document class from the top-level? e.g.
class User < ActiveRecord::Base
include PgSearch
def network_documents
::Document.where(...) # note the 2 colons in front
end
end
It is name spaced, but as soon as you include PgSearch the name spacing is rendered moot. I've submitted pull request 96 that renames the class to something less generic, to avoid any possibility of conflict.
Ok. But renaming PgSearch::Document
to PgSearch::SearchDocument
will work until someone has a model named SearchDocument
in a similar scenario to your Document
. In other words there is still possibility for conflict unless one uses an absolute reference to the constant. So prefixing ::
to Document
should give you the behavior you're after unless there is something else going on.
You're right, but that is a more specific, less likely scenario, especially in an environment where pg_search is being used. We could go as far as naming it PGSearchDocument to be sure. I've been involved in a lot of projects involving file uploading/management and there's almost always an AR model named Document. This is actually the first time I've run into a class name conflict from a gem, so not sure if there's a different solution to including PgSearch and exposing its own Document class.
Copying over comment from #96 that is also relevant here:
I'd rather work towards removing the need for an Active Record model to live in PgSearch at all. I don't want to break existing code that might assume the PgSearch::Document class is there.
Let's move toward a generator that generates models that the developer chooses the name for, and remove the idea of a single "Document" class once and for all.
There's a discussion going on over at #92 towards this.
I've also been hit by this a few times, as the issue is still present three years later. It would be great to see a fix for this.
After spending a good three hours trying to track down why this was happening to me, I'd also like to chip in and say this should rename into something a bit less generic.
Its been a long day, so I may be missing something completely obvious, but I ran into this very strange error today.
I have two models named User and Document that both have search scopes. A user has many documents.
I wanted to add a method to user to get all documents available in his network. It looked something like this:
user.rb
So that I can call something like:
Normal enough right? However, any time I try to add a method to the User or Document model that refers to Document, I get the error listed in the issue subject along with a stack trace that looks like this:
If I remove the include and search scope, everything runs fine as expected. I tried adding the method as a class method to the Document class with the same result.
What the hell am I missing? Cheers.