我有一个简单的层次结构的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