一個快速的問題:軌道4:型號邏輯 - 由用戶計數物品
讓我們假設我有一個用戶(設計)&產品型號。 current_user可以創建並標記他的項目。如果他這樣做,則is_marked
的值更改爲true
。
工作。
但是,我怎樣才能收到由current_user
標記的物品的數量?
我想是這樣的:
current_user.joins(:items).where(user.id: current_user.id)
# How can I count the number of items marked?
# In addition, I don't know if it is the right solution to use joins. Correct me if I am wrong.
協會: 用戶:has_many :items
項目:belongs_to :user
預先感謝任何答案!如果您需要更多信息,請告訴我。
你有beetwen這些模型的關聯? –
'current_user.items.where(is_marked:true).count' – fanta
@BartekGładys編輯後。 – Gugubaight