![](/img/trans.png)
[英]Query for Records beginning with “The” and move “The” the end of a string and join with a comma in Rails Postgres
[英]Postgres Join Query on Rails
我進行了很多研究,但找不到我的問題的答案。 我在Rails應用程序上進行了以下設置:
class Group < ActiveRecord::Base
has_many :people
# ...
end
class City < ActiveRecord::Base
has_many :people
# ...
end
class Person < ActiveRecord::Base
belongs_to :city
belongs_to :group
# ...
end
人們的列:role
為0
或1
。
我想獲得所有至少具有一個role == 0
人和一個role == 1
。
任何想法? 我正在使用Postgres。
這是我剛剛在SQLite3數據庫上測試過的查詢(我相信也應該在Postgres上工作):
Group.select("groups.*").joins("LEFT JOIN people on groups.id = people.group_id").where("people.role==0 OR people.role==1").group("id")
在這里,我假設您已經在人員遷移中添加了外鍵group_id。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.