我有一个由Addresses
、Business
和Individuals
组成的地址簿。这三个模型都具有 HABTM 关系,因为这就是现实的运作方式。
每个地址也有地理坐标(Address.latitude
和Address.longitude
)。
我想在一个特殊点 (/london
) 为我的 Rails Controller 获取一个集合,在一个集合中,所有企业 和 拥有最终出现在传单 map 上的伦敦地址。
但我也不想要伦敦以外的任何地址。
目前,
class Individual < ActiveRecord::Base
has_and_belongs_to_many :addresses, join_table: :addresses_individuals
has_and_belongs_to_many :businesses, join_table: :businesses_individuals
scope :london, -> { joins(:addresses).where("addresses.latitude < ? AND addresses.latitude > ? AND addresses.longitude < ? AND addresses.longitude > ?", $London[:north], $London[:south], $London[:east], $London[:west]).distinct }
def js_slug
"#{self.first_name}_#{self.last_name}".parameterize.underscore.camelize(:lower)
end
end
我在 Controller 中访问 @individuals = Individual.london
,它会给我所有有伦敦地址的个人,但如果有人也有伦敦以外的地址,那么它会被收集进入列表(如果我缩小,最终会出现在 map 上)。
我为 Address
做了一个类似的作用域:
class Address < ActiveRecord::Base
has_and_belongs_to_many :individuals, join_table: :addresses_individuals
has_and_belongs_to_many :businesses, join_table: :businesses_addresses
scope :london, -> { where("addresses.latitude < ? AND addresses.latitude > ? AND addresses.longitude < ? AND addresses.longitude > ?", $London[:north], $London[:south], $London[:east], $London[:west]) }
end
现在,
@individuals = Address.london.map{ |a| a.individuals }
接近我想要的,至少在控制台中是这样,但是当我将其放入 Controller 时:
class Frontend::FrontendController < ApplicationController
layout :resolve_layout
respond_to :html
def london
@individuals = Address.london.map{ |a| a.individuals }
# @individuals = Individual.london
@fitbounds = [[$London[:south], $London[:west]], [$London[:north], $London[:east]]]
@circle_markers = nil
render "city"
end
View :
<table class="table table-striped table-hover table-condensed table-responsive">
<tbody>
<% @individuals.each do |individual| %>
<tr>
<td id="<%= individual.js_slug %>">
<%= link_to "#{individual.first_name} #{individual.last_name}", [:frontend, individual] %>
</td>
</tr>
<% end %>
</tbody>
</table>
出错,我得到:
undefined method `js_slug' for Individual::ActiveRecord_Associations_CollectionProxy:Class
当我使用 @individuals = Individual.london
时,对 js_slug
的调用工作正常,但随后我在 map 上得到了所有不在伦敦的地址。
因此,一旦通过 @individuals = Individual.london
收集并丢弃伦敦以外的地址,而无需遍历每个地址,我如何才能获得如此有限的集合?
那么当我对企业做同样的事情时,我如何才能将它与个人集合合并/加入?
谢谢!
最佳答案
问题是 Address.london.map{ |a| a.individuals } 返回单个元素,即个体的集合(试试@individuals.count,应该等于1)。
本质上,我们在这里追求的是连接表中 address_id 属于伦敦某个地址的所有记录,并且我们想要显示属于 individual_id 的个人。因此,我认为我们可以使用连接表中的范围来达到您想要的效果,如下所示。
# address_individual model (sim for business)
class AddressIndividual < ActiveRecord::Base
scope :london, -> { where("address_id in (?)", Address.london.map(&:id)) }
end
然后我们可以在 Controller 中调用它。我会使用与“个人”不同的名称以避免混淆(我们将在下面的 View 中定义个人)。我将只使用“address_individuals”。
# individual controller
class Frontend::FrontendController < ApplicationController
layout :resolve_layout
respond_to :html
def london
@address_individuals = AddressIndividual.london
@fitbounds = [[$London[:south], $London[:west]], [$London[:north], $London[:east]]]
@circle_markers = nil
render "city"
end
end
然后我们可以在 View 中引用它,只需要一个额外的步骤来显示个体。
<table class="table table-striped table-hover table-condensed table-responsive">
<tbody>
<% @address_individuals.each do |ai| %>
<% individual = Individual.find(ai.individual_id)
<tr>
<td id="<%= individual.js_slug %>">
<%= link_to "#{individual.first_name} #{individual.last_name}", [:frontend, individual] %>
</td>
</tr>
<% end %>
</tbody>
</table>
我认为这对你有用。您可以为企业做同样的事情。然后我不确定如何将它们组合到单个范围或事件记录查询中 - 我会在您的 View 中为每个循环执行两个循环。
关于ruby-on-rails - 基于第三个 HABTM 模型的标准,来自两个模型的单个 ActiveRecord 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29714820/