从Rails模型中访问current_user?

我正在构建一个基本遵循以下模式的Invoicing应用程序:

Users < Clients < Projects < Invoices

现在,为了为每个用户生成自动递增发票号,我将其放入我的Invoice模型中:

  before_create :create_invoice_number def create_invoice_number val = @current_user.invoices.maximum(:number) self.number = val + 1 end 

但是,似乎无法从Rails中的模型中访问current_user变量?

我该怎么做才能解决这个问题?

这是由于Rails中的问题分离,并且是一个有点棘手的问题需要处理。 在Rails范例中,模型应该不知道任何应用程序状态超出它们直接传递的范围,因此大多数Rails编码器会告诉您任何需要了解current_user的模型都是代码味道。

也就是说,有三种方法可以做到这一点,每种方法都“更正确”(或者至少我会这么认为)。

首先,尝试在发票内创建与用户的关联,并将发票链接到控制器中的用户:

 class InvoicesController < ApplicationController ... def create @invoice = current_user.invoices.create(params[:invoice]) ... end 

在你的模型中:

 belongs_to :user def create_invoice_number self.user.invoices.maximum(:number) + 1 end 

如果这不起作用,请在控制器中手动执行此操作。 确实,控制器应该总是像你可以管理的一样瘦,但由于这显然是应用程序级别的关注,控制器是放置它的地方:

 class InvoicesController < ApplicationController ... def create @invoice = Invoice.create(params[:invoice]) @invoice.update_attribute(:number, current_user.invoices.maximum(:number)) ... end 

最后,如果你真的想要桥接控制器和模型,你可以使用ActionController :: Sweepers 。 它们并非用于此目的,但肯定会为您完成工作。

如果你想在铁轨中使用observers ,那么这种情况不应该出现