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

发布于 2024-10-14 06:56:16 字数 1314 浏览 1 评论 0原文

我试图使搜索变得非常复杂(当然,是为了让用户更容易)

我有一个具有 3 个模型的应用程序:营销活动、企业和位置,

如下所示:

\\ 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

按照这种设置方式,有一些企业有多个地点。对于那些不这样做的人,geokit 信息将被编码到活动数据库条目中。对于那些确实有多个位置的情况,geokit 信息被编码到位置数据库条目中。

我正在尝试搜索将在一定距离内返回结果的活动。当处理具有单一地址的企业时,这很简单。

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

不过,我还想包括属于拥有多个地点的企业的营销活动。如果该企业有多个地点,并且其中任何一个地点位于范围内,我希望搜索返回该营销活动的结果。我在想:

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

但这不会为属于拥有多个地点的企业的活动返回任何结果。当涉及到 SQL 时,我是个菜鸟,所以我不太确定如何在一个模型(营销活动)中进行 Rails 搜索,并在另一个模型(业务模型)中进行搜索以从位置模型中获取结果。事实上,涉及到 geokit 使得事情变得更加复杂。

我尝试过:acts_as_mappable :through => :locations 在campaign.rb中,但它只是抛出了一个sql错误

,我用一个多态模型“可寻址”搞乱了,但我发现我几乎必须在其他模型的控制器上从头开始。

我也考虑过named_scopes,但我相信geokit不支持它们。

有什么建议吗?

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

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. For those that do have multiple locations the geokit info is coded into the location database entry.

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. The fact that geokit is involved makes it even more complex.

I tried: acts_as_mappable :through => :locations in the campaign.rb but it just threw an sql error

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.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-10-21 06:56:16

如果您认为您的模型过于复杂,则表明它需要重新设计。

没有企业是否可以开展活动?如果没有,那么如果企业已经有可映射的位置,那么将营销活动设置为 act_as_mappable 是没有意义的(我假设您的营销活动数据库具有纬度和经度列)。

如果营销活动与具有多个地点的企业相关联,则该营销活动的位置被视为什么?您在 Campaign.lat 和 lng 属性中存储什么?

如果您坚持使用数据模型,我建议将搜索分解为多个命令:

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

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.

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?

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文