Ruby on rails 如何";“合并”&引用;“重复”;活动记录对象?

Ruby on rails 如何";“合并”&引用;“重复”;活动记录对象?,ruby-on-rails,ruby,activerecord,google-maps-api-3,Ruby On Rails,Ruby,Activerecord,Google Maps Api 3,所以,我正在建立一个地图,我把引脚(谷歌地图) 我有一个查询,给我我的引脚位置,但我可以有多个引脚与完全相同的位置,但不同的描述 从今天早上开始,我就一直把头撞在桌子上,不知什么原因,我找不到有效的解决方案 以下是我到目前为止的情况: building_permits = BuildingPermit.select('latitude, longitude, street, city, state, permit_number, description, type, id').where(:co

所以,我正在建立一个地图,我把引脚(谷歌地图)

我有一个查询,给我我的引脚位置,但我可以有多个引脚与完全相同的位置,但不同的描述

从今天早上开始,我就一直把头撞在桌子上,不知什么原因,我找不到有效的解决方案

以下是我到目前为止的情况:

building_permits = BuildingPermit.select('latitude, longitude, street, city, state, permit_number, description, type, id').where(:contractor_id => params[:nid])
@bp = Array.new
building_permits.each do |bp|
  @bp.push({"lat" => bp.latitude, "lng" => bp.longitude, "desc" => "<p><b>#{bp.street} #{bp.city}, #{bp.state}</b></p><p><b>Description:</b>#{bp.description}</p><p><b>Permit #{bp.permit_number}</b></p><p><b>#{bp.type}</b></p>", "id" => bp.id})
end
nb_rm = 0
building_permits.each_with_index do |bp, index|
  index1 = index
  building_permits.each_with_index do |bp2, index|
    if bp.longitude == bp2.longitude && bp.latitude == bp2.latitude && bp.id != bp2.id
      #debugger
      if @bp[index1].present?
        @bp[index1]["desc"] << "<br /><br /><b>Description:</b>#{bp2.description}</p><p><b>Permit #{bp2.permit_number}</b></p><p><b>#{bp2.type}</b></p>"
        @bp.delete_at(index-nb_rm)
        nb_rm += 1
      end
    end
  end
end
building_-permissions=buildingpermission。选择(“纬度、经度、街道、城市、州、许可证编号、描述、类型、id”)。其中(:contractor_-id=>params[:nid])
@bp=Array.new
建筑许可证。每个都有|
@bp.push({lat=>bp.latitude,“lng=>bp.longitude,“desc”=>“{bp.street}{bp.city},{bp.state}

描述:{bp.Description}

许可证{bp.Permit bp.Permit number}

许可证{bp.Permit bp.type}

,“id”

.id}) 结束 nb_rm=0 建筑许可证。每个许可证都有索引do | bp,索引| index1=索引 建筑许可证。每个许可证都有索引do | bp2,索引| 如果bp.longitude==bp2.longitude&&bp.latitude==bp2.latitude&&bp.id!=bp2.id #调试器 如果@bp[index1],是否存在?
@bp[index1][“desc”]尝试复制pin位置:

# rails < 3.1
new_record = old_record.clone
#rails >= 3.1
new_record = old_record.dup
#and then
new_record.save
#rails<3.1
新记录=旧记录。克隆
#轨道>=3.1
新记录=旧记录.dup
#然后
new_record.save

毕竟更改了pin的描述。

我遇到了类似的问题,想发布我的解决方案——我有重复的作者,想找到名称属性相等的任何作者,并将相关对象(书籍和推荐)移动到每个重复组的第一个,然后删除其他人

Author.all.each do |a|
@name = a.name
if Author.where(:name => @name).count > 1
    first = Author.where(:name => @name).first
    a.books.each{|b| b.update_attributes(author_id: first.id)}
    a.recommendations.each{|r| r.update_attributes(author_id: first.id)}
end
end

Author.all.each do |a|
if a.books.count == 0 && a.recommendations.count == 0
    a.delete
end
end

希望这能对某人有所帮助

如果您能用文字解释一下您希望实现的合并规则,这将有所帮助。