【问题标题】:Ruby on Rails sort on prioritiesRuby on Rails 按优先级排序
【发布时间】:2016-04-13 01:28:32
【问题描述】:

以下是我获取国家和城市列表的查询:

@countries_cities = Product.joins(:user).where("country is not null and country <> ''").where("city is not null and city <> ''").where(:users => {:merchant_status => 1}).group(:country, :city).select("country,city").as_json

输出结果如下:

Object[city:"Bangkok",country:"Thailand"],Object[city:"Phuket",country:"Thailand"],Object[city:"Malaysia",country:"Kuala Lumpur"],Object[city:"Malaysia",country:"Penang"],Object[city:"Shanghai",country:"China"],Object[city:"Beijing",country:"China"]

cchs = @countries_cities.group_by{|cc| cc["country"]}
@search_location_country = cchs

而观点是:

    <ul id="color-dropdown-menu" class="dropdown-menu dropdown-menu-right" role="menu">
      <% @search_location_country.each do |country, cities| %>
        <li class="input" style="background:#ECECEC; "><a href="#" style="font-weight: bold;"><%= country.upcase %></a></li>
                <%  cities.each do |city| %>
        <li class="input"><a href="#"><%= city["city"].titleize %></a></li>
    <% end %>
   <% end %>
</ul>

现在下拉结果遵循以下模式:

Thailand
  -Bangkok
  -Phuket
Malaysia
  -Kuala Lumpur
  -Penang
China
  -Beijing
  -Shanghai

如何确保Malaysia 始终位于下拉列表的顶部?谢谢!!

【问题讨论】:

  • 您使用的是什么 SQL? MySQL、Postgre?
  • @magni- 我正在使用 Postgres

标签: ruby-on-rails ruby


【解决方案1】:

怎么样:

@countries_cities = Product.joins(:user)
                           .where.not(country: [nil, ''])
                           .where(users: {merchant_status: 1})
                           .group(:country, :city)
                           .order("country!= 'Malaysia'")
                           .select(:country, :city)
                           .as_json

在 Postgres 中,false 排在 true 之前(在此处查看此答案:Custom ORDER BY Explanation

【讨论】:

  • 啊,现在可以了!!怎么设置country!= 'Malaysia'Malaysia 排在首位?
  • 查看链接以获得更详细的解释,但基本上:具有countryMalaysia 的行将具有country!= 'Malaysia' 评估为false,并且这些行在其中的行上方排序计算结果为true
【解决方案2】:

您可以像这样自定义查询:

@countries_cities = Product.joins(:user)
                           .where.not(country: [nil, ''])
                           .where(:users => {:merchant_status => 1})
                           .group(:country, :city)
                           .order("ORDER BY 
                                   CASE WHEN country = 'Malaysia' THEN 1
                                   ELSE 2
                                   END ASC")
                           .select(:country, :city)
                           .as_json

所以我们设置了马来西亚 = 2,其他 = 1 的顺序,以确保马来西亚的结果会在最前面。

【讨论】:

  • 不,结果似乎显示顺序没有变化
  • @d3bug3r:嗯,我只是修改了我的答案,让我们再试一次!
  • 好的,它现在告诉我一些错误PG::SyntaxError: ERROR: syntax error at or near "ORDER" LINE 1: ... "products"."country", "products"."city" ORDER BY ORDER BY
  • : SELECT "products"."country", "products"."city" FROM "products" INNER JOIN "users" ON "users"."id" = "products"."user_id" WHERE (NOT (("products"."country" = '' OR "products"."country" IS NULL))) AND "users"."merchant_status" = $1 GROUP BY "products"."country", "products"."city" ORDER BY ORDER BY CASE WHEN country = 'Malaysia' THEN 1 ELSE 2 END ASC
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 2011-10-09
  • 2012-06-07
  • 1970-01-01
相关资源
最近更新 更多