Rails 3, ActiveRecord, PostgreSQL - “.uniq” command doesn't work?

后端 未结 4 1330
轮回少年
轮回少年 2020-12-09 17:51

I have following query:

Article.joins(:themes => [:users]).where([\"articles.user_id != ?\", current_user.id]).order(\"Random()\").limit(15).uniq
<         


        
4条回答
  •  庸人自扰
    2020-12-09 18:05

    Just to enrich the thread with more examples, in case you have nested relations in the query, you can try with the following statement.

    Person.find(params[:id]).cars.select('cars.*, lower(cars.name)').order("lower(cars.name) ASC")
    

    In the given example, you're asking all the cars for a given person, ordered by model name (Audi, Ferrari, Porsche)

    I don't think this is a better way, but may help to address this kind of situation thinking in objects and collections, instead of a relational (Database) way.

    Thanks!

提交回复
热议问题