我目前在rails 4和postgres上使用ruby,并按照下面的代码进行区分和排序。
我只是感觉不对,而且没有优化,因为它似乎是通过数组来完成所有这些工作。
如何将所有这些组合到一个activerecords查询中?
ads1 = Advertisement.where(property_id: nil)
.where(target_address_city: [property.address_city, ""],
target_address_state: [property.address_state, ""],
target_address_country_id: property.address_country_id)
# return advertisement belongs to the property
ads2 = management.property.advertisements
#combine both results
combine_ads = ads1 + ads2
#remove ads duplicate
uniq_ads = combine_ads.uniq { |ads| ads.id}
# sort by created_at desc
uniq_ads = uniq_ads.sort_by { |ads| -ads[:id]}
# do pagination
final_ads = uniq_ads.paginate(:page => params[:page], :per_page => params[:page_limit])