我有三個型號:named_scope或find_by_sql?
- 用戶
- 獎
- 獎盃
的關聯是:
- 用戶擁有衆多獎項
- 獎盃有許多獎項
- 獎屬於用戶
- 獎屬於獎盃
- 用戶已經通過獎勵
許多獎盃因此,user_id是在獎勵FK,和trophy_id是在獎勵FK。
在Trophy模型中,這是一個STI模型,它有一個trophy_type列。我想返回已經獲得特定獎盃的用戶列表 - (trophy_type ='GoldTrophy')。用戶可以多次被授予同一獎盃。 (我不想要明顯的結果。)
我可以使用named_scope嗎?如何鏈接他們?或者我需要使用find_by_sql?無論哪種方式,我將如何編碼?