简体   繁体   English

使用geokit问题在Rails中进行基于位置的搜索

[英]Location based search in Rails using geokit problem

I have a rather complex (or so it seems) geokit based location search on my site. 我的网站上有一个基于geokit的相当复杂(或看起来如此)的位置搜索。 In short, I have a model named "Campaigns" that belongs to another model called "Businesses". 简而言之,我有一个名为“ Campaigns”的模型,该模型属于另一个名为“ Businesses”的模型。 When I am searching on the site, users are searching for "Campaigns", but I want all appropriate models to show up in the results if they search for that Campaigns business name. 当我在网站上搜索时,用户正在搜索“广告系列”,但是如果他们搜索该Campaigns公司名称,我希望所有合适的模型都显示在结果中。 For this I am doing a joins in the search. 为此,我正在进行搜索联接。

I additionally have the geokit plugin and gem installed so that users can get results for these searches only within a set distance from the origin location (which they provide). 此外,我还安装了geokit插件和gem,以便用户只能在距原始位置(他们提供的位置)一定距离内才能获得这些搜索的结果。 However, I am getting strange results when I add this location functionality into the site. 但是,当我将此定位功能添加到站点中时,却得到了奇怪的结果。

If I use the following search (simplified for brevity, but tested in the console): 如果我使用以下搜索(为了简洁起见,但在控制台中对其进行了测试):

Campaign.find(:all, 
              :joins => :business, 
              :conditions => ['businesses.name LIKE ?', "%Koo Koo Roo%"]
             )

I get the appropriate result, which is: 我得到适当的结果是:

[#<Campaign id: 12, user_id: 4, business_id: 8, created_at: "2011-01-14 16:22:31", updated_at: "2011-01-14 16:25:20", lat: #<BigDecimal:2ad295a9eda8,'0.34154891E2',18(18)>, lng: #<BigDecimal:2ad295a9ed08,'-0.118358834E3',18(18)>>]

But if I try to add geokit based location search parameters onto this search, like so: 但是,如果我尝试将基于geokit的位置搜索参数添加到此搜索中,如下所示:

Campaign.find(:all, 
              :joins => :business, 
              :origin => "90066", 
              :within => 25, 
              :conditions => ['businesses.name LIKE ?', "%Koo Koo Roo%"]
             )

I get the following result: 我得到以下结果:

[#<Campaign id: 8, user_id: 4, business_id: 8, created_at: "2011-01-14 16:25:20", updated_at: "2011-01-14 16:25:20", lat: #<BigDecimal:2ad29933e618,'0.34154891E2',18(18)>, lng: #<BigDecimal:2ad29933e578,'-0.118358834E3',18(18)>>]

Which is almost identical. 这几乎是相同的。 The only difference is, for the second result, it seems to be passing the business_id as the Campaign id. 唯一的不同是,对于第二个结果,它似乎正在传递business_id作为Campaign ID。 I have verified this twice, and both times it is the same thing, the campaign id gets replaced with the business_id. 我已经验证了两次,两次都一样,广告系列ID被替换为business_id。

I should mention, that this is true no matter what :within distance i enter. 我应该提到的是,无论什么,这都是正确的:在我进入的距离之内。

What am I doing wrong here? 我在这里做错了什么? Am I missing something? 我想念什么吗? I can't seem to figure it out, it all looks sound to me, but apparently not! 我似乎无法弄清楚,对我来说一切都听起来不错,但显然不! I don't understand how the results could be screwed up like this by geokit. 我不明白geokit如何将结果搞砸了。

in my models I simply have: 在我的模型中,我只是有:

Business.rb
has_many :campaigns

Campaign.rb
belongs_to :business

Any help would be appreciated. 任何帮助,将不胜感激。 Am I missing some sort of association? 我想念某种联系吗? I don't have geokit caching turned on. 我没有打开geokit缓存。

Thanks! 谢谢!

It's because the "id" field is being overwritten by dodgy join code. 这是因为“ id”字段已被狡猾的联接代码覆盖。 When you join, you've got two "id" fields (one for business and one for campaign). 加入后,您将拥有两个“ id”字段(一个用于业务,一个用于广告系列)。 without an explicit instruction as to which is "the" id, the DB guesses. 数据库猜测,如果没有明确的指令说明哪个ID是“ the”。

Unfortunately, the one of the versions of rails has a bug where it did not explicitly state that the main Active Record id (in this case Campaign.id) was the id that counts... and the db was guessing the wrong one, overwriting id with the Business.id. 不幸的是,其中一个版本的rails有一个错误,即它没有明确指出主要的Active Record ID(在本例中为Campaign.id)是很重要的ID。与Business.id的ID。

You've already discovered the easy (but more hacky) fix... the other is upgrading rails. 您已经发现了简单(但更加棘手)的修复程序...另一个是升级rails。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM