使用acts_as_taggable_on 查询错误表名来测试Thinking Sphinx
首先,生产方面工作得很好,只有当我运行测试时才会出现错误。
正如标题中所述,我将 Thinking Sphinx 与acts_as_taggle gem 一起使用。以下是我正在使用的模型中的一些片段:
class Special < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :cuisines, :meals
...
define_index do
...
indexes cuisine_taggings.tag(:name), :as => :cuisine_tags, :facet => true
indexes meal_taggings.tag(:name), :as => :meal_tags
...
end
...
end
搜索看起来像这样:
Special.search("some query", {
:conditions => {:meal_tags => "dinner"} ,
:geo => coordinates,
:retry_stale => true,
:with => {
"@geodist" => 0.0..distance,
:timeframe => [1,2],
},
:page => page,
:per_page => Kaminari.config.default_per_page,
:order => "#{day} desc, @relevance DESC, @geodist ASC"
}
我看到的错误是这样的:
Mysql2::Error: Unknown column 'cuisine_taggings_specials.context' in 'where clause': SELECT `taggings`.`id` AS t0_r0, `taggings`.`tag_id` AS t0_r1, `taggings`.`taggable_id` AS t0_r2, `taggings`.`taggable_type` AS t0_r3, `taggings`.`tagger_id` AS t0_r4, `taggings`.`tagger_type` AS t0_r5, `taggings`.`context` AS t0_r6, `taggings`.`created_at` AS t0_r7, `tags`.`id` AS t1_r0, `tags`.`name` AS t1_r1 FROM `taggings` LEFT OUTER JOIN `tags` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 1 AND `taggings`.`taggable_type` = 'Special' AND (`cuisine_taggings_specials`.context = 'cuisines')
如您所见,查询试图找到“cuisine_taggings_specials”表,而不仅仅是标记表。
每当我更改索引时,我都会在开发和测试中重建索引。
就像我说的,生产和开发工作得很好,只是当我运行测试时。
To begin with, the production side of things works just fine, it's only when I run the tests that errors arise.
As stated in the title, I am using Thinking Sphinx with the acts_as_taggle gem. Here are some snippets from the model I am using:
class Special < ActiveRecord::Base
acts_as_taggable
acts_as_taggable_on :cuisines, :meals
...
define_index do
...
indexes cuisine_taggings.tag(:name), :as => :cuisine_tags, :facet => true
indexes meal_taggings.tag(:name), :as => :meal_tags
...
end
...
end
The search looks something like this:
Special.search("some query", {
:conditions => {:meal_tags => "dinner"} ,
:geo => coordinates,
:retry_stale => true,
:with => {
"@geodist" => 0.0..distance,
:timeframe => [1,2],
},
:page => page,
:per_page => Kaminari.config.default_per_page,
:order => "#{day} desc, @relevance DESC, @geodist ASC"
}
The errors I'm seeing are this:
Mysql2::Error: Unknown column 'cuisine_taggings_specials.context' in 'where clause': SELECT `taggings`.`id` AS t0_r0, `taggings`.`tag_id` AS t0_r1, `taggings`.`taggable_id` AS t0_r2, `taggings`.`taggable_type` AS t0_r3, `taggings`.`tagger_id` AS t0_r4, `taggings`.`tagger_type` AS t0_r5, `taggings`.`context` AS t0_r6, `taggings`.`created_at` AS t0_r7, `tags`.`id` AS t1_r0, `tags`.`name` AS t1_r1 FROM `taggings` LEFT OUTER JOIN `tags` ON `tags`.`id` = `taggings`.`tag_id` WHERE `taggings`.`taggable_id` = 1 AND `taggings`.`taggable_type` = 'Special' AND (`cuisine_taggings_specials`.context = 'cuisines')
As you can see, the query is trying to locate the "cuisine_taggings_specials" table instead of just the taggings table.
I rebuild the indexes in both dev and test whenever I change the index.
Like I said, production and development work just fine, it's only when I'm running my tests.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想知道这是否与测试环境生成配置时未填充多态数据有关。你如何进行测试?黄瓜,纯水豚,还是其他什么?
添加一些数据后,尝试重新生成配置,然后建立索引:
当然,如果错误发生在您的设置之外,这没有帮助......因此第一个问题:)
I wonder if it's related to polymorphic data not being populated when the test environment is generating the configuration. How are you running the tests? Cucumber, straight Capybara, or something else?
Once you've added some data, try regenerating the configuration and then indexing:
Of course, this isn't helpful if the error's happening outside of your setup... Hence the first question :)