简体   繁体   English

使用Rails进行多种模型和Geokit的复杂搜索

[英]Complex Search with Multiple Models and Geokit using Rails

I am attempting to make a search very complicated (of course, to make it easier for users) 我正在尝试使搜索变得非常复杂(当然,这会使用户更轻松)

I've got an app with 3 models: Campaigns, Businesses and Locations 我有一个具有3种模型的应用程序:广告系列,企业和位置

like so: 像这样:

\\ campaign.rb
  belongs_to :business
  has_many :locations, :through => :business
  acts_as_mappable

\\ business.rb
  has_many :campaigns
  has_many :locations

\\ location.rb
  belongs_to :business
  has_many :campaigns, :through => :business
  acts_as_mappable

The way this is set up, there are some businesses that have multiple locations. 设置方式中,有些企业位于多个位置。 For those that don't, the geokit info is coded into the campaign database entry. 对于那些没有的人,geokit信息被编码到活动数据库条目中。 For those that do have multiple locations the geokit info is coded into the location database entry. 对于确实具有多个位置的地理位置,geokit信息被编码到位置数据库条目中。

I am trying to do a search for campaigns that will return the results within a certain distance. 我正在尝试搜索将在一定距离内返回结果的广告系列。 This is simple enough when dealing with businesses that have a single address. 与具有单个地址的企业打交道时,这很简单。

  Campaign.find(:all,  
       :conditions => [blahblahblah],
       :origin => address,
       :within => distance
       )

However, I want to also include campaigns that belong to businesses that have multiple locations. 但是,我还想包括属于具有多个位置的企业的广告系列。 I want the search to return a result for that campaign, if the business has multiple locations, and if any of those locations fall within the bounds. 如果企业有多个地点,并且其中任何一个地点在范围之内,我希望搜索返回该广告系列的结果。 I was thinking something like: 我在想类似的东西:

  Campaign.find(:all, 
       :include => [:business, :locations]
       :conditions => [blahblahblah],
       :origin => address,
       :within => distance
       )

But this doesn't return any results for campaigns that belong to businesses that have multiple locations. 但这不会返回属于具有多个位置的公司的广告系列的任何结果。 I'm a noob when it comes to SQL so I'm not exactly sure how to have rails search in one model (Campaign), and search across another model (the Business model) to grab results from the Location model. 对于SQL,我是一个菜鸟,所以我不确定如何在一个模型(广告系列)中搜索rails,并在另一个模型(业务模型)中进行搜索以从Location模型中获取结果。 The fact that geokit is involved makes it even more complex. 涉及geokit的事实使其更加复杂。

I tried: acts_as_mappable :through => :locations in the campaign.rb but it just threw an sql error 我试过了:campaign.rb中的acts_as_mappable :through => :locations ,但是它只是抛出了一个SQL错误

I messed around with a polymorphic model "addressable" but I found I would pretty much have to start from the ground up on the controllers of the other models. 我把“可寻址”的多态模型弄乱了,但是我发现我几乎必须从其他模型的控制器开始。

I've also thought about named_scopes but I believe that geokit does not support them. 我也考虑过named_scopes,但我相信geokit不支持它们。

Any suggestions? 有什么建议么?

If you think your model is overly complicated its a good sign that it needs a redesign. 如果您认为模型过于复杂,则表明它需要重新设计。

Is it possible to have a campaign without a business? 可以开展没有生意的竞选活动吗? if not, then it doesnt make sense to make campaign act_as_mappable (Im assuming your campaign DB has lat and lng columns) if business already has mappable locations. 如果不是,那么如果业务已经具有可映射的位置,则使活动act_as_mappable(假设您的活动数据库具有lat和lng列)没有意义。

If campaign is associated with a business that has multiple locations, whats considered location of the campaign? 如果广告系列与具有多个位置的企业相关联,那么该广告系列的位置是什么? What do you store in campaigns.lat and lng attributes? 您在campaigns.lat和lng属性中存储了什么?

If youre sticking with your datamodel, I recommend to break up your search into multiple commands: 如果您坚持使用数据模型,建议您将搜索分为多个命令:

def find_campaigns_near(origin,distance)
    locations = Location.find(:all,  
            :origin => address,
            :within => distance
    );
    # use Hash for uniqueness based on id
    campaigns = Hash.new
    locations.each do |location|
        location.campaigns.each do |campaign|
            campaigns[campaign.id] = campaign if campaigns[campaign.id].blank?
        end
    end
    campaigns
end

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

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