我有一個簡單的層次結構的rails 3.2應用程序:用戶有很多客戶端,客戶端有很多發票。在範圍內訪問父項
我希望用戶只能通過做Client.by_user(current_user)
和Invoice.by_user(current_user)
來查看他們自己的客戶和使用範圍的發票。對於客戶來說,我有這個,它工作得很好:但
scope :by_user, lambda { |user| where(user_id: user.id) }
,如果我嘗試在同一發票
scope :by_user, lambda { |user| where(client.user_id => user.id) }
失敗的話,告訴我undefined local variable or method 'client'
。
我在做什麼錯?我不想將user_ids添加到發票中。
爲什麼不只是使用'current_user.clients'和'current_user.invoices'? (編輯:假設'User''has_many:invoices,:through =>:clients') – gregates 2013-02-15 20:44:13
我試過了,但它沒有識別'invoice.user'。我必須將user_id添加到發票表中嗎? – weltschmerz 2013-02-15 20:48:04
你不應該。您可以在發票''belongs_to:user,:through =>:client''處做反向關係。 – gregates 2013-02-15 20:50:05