0
我在has_many上組合多個外鍵時遇到了問題。我明白,這不是has_many上foreign_key的明確意圖。但無論我在尋找一種簡單的方法來組合兩個外鍵。Ralis Multiple foreign_key
我試着將兩個的has_many的,像這樣:
has_many :primary_clients, foreign_key: :coach_id, inverse_of: :coach do
end
has_many :secondary_clients, foreign_key: :coach_two_id, inverse_of: :coach do
end
def clients
Client.where("primary_clients = ? OR secondary_clients = ?", self.id, self.id)
end
而且我也試過unscope重新定義範圍:
has_many :clients, ->(user) { unscope(:where).where("coach_id = :id OR coach_two_id = :id", id: ids) }, inverse_of: :coach do
end
我喜歡的東西很簡單,像後是:
has_many :clients, foreign_key: {:coach_id, :coach_two_id}, inverse_of: :coach do
end
任何意見,以正確的方向指向我?