Location based search in Rails using geokit problem
I have a rather complex (or so it seems) geokit based location search on my site. In short, I have a model named "Campaigns" that belongs to another model called "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. 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). 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:
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:2开发者_如何学C0", 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. I have verified this twice, and both times it is the same thing, the campaign id gets replaced with the 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.
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.
Thanks!
It's because the "id" field is being overwritten by dodgy join code. When you join, you've got two "id" fields (one for business and one for campaign). without an explicit instruction as to which is "the" id, the DB guesses.
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.
You've already discovered the easy (but more hacky) fix... the other is upgrading rails.
精彩评论