我有以下型号:
class Document < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :user
belongs_to :authentication
class Authentication < ActiveRecord::Base
belongs_to :user
has_many :documents
我可以在我的 Controller 中查询 Elasticsearch ,如下所示:
@documents = Document.search params[:q]
@documents 已正确填充,但我不能再像这样使用 ActiveRecord 关联:
@documents.first.authentications.name
我收到以下错误:
undefined method `authentication' for #<Elasticsearch::Model::Response::Result:0x007fd07929f5a8>
我怎样才能完成这项工作,或者有更好的方法来处理这种需求?
最佳答案
我认为您在调用 @documents.first.authentications.name
时有错字。 .请注意,您有一个关联 belongs_to :authentication
.
请尝试@documents.results
或 @documents.records
在调用 first
之前.
所以试试@documents.records.first.authentications.name
.
关于ruby-on-rails - 如何在 ElasticSearch 中使用 Active Record 关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38133328/