1

我开始了一个新项目,该项目要求帐户能够查看其候选人和相关任务。

帐号.rb

has_many :candidates

候选人.rb

has_many :tasks
belongs_to :account

任务.rb

belongs_to :candidate

路由设置为允许 /candidates/4/tasks/3 帐户 X 可以访问具有任务 3 的候选人 4。

tasks_controller.rb 目前是这样的:

def show
  @task = Task.find params[:id]
end

问题:确保其他帐户无权访问任务 3 的最佳做法是什么?

一个想法可能是这样的,但看起来很混乱:

def show
  @task = Account.find(current_account).candidates.find(params[:candidate_id]).tasks.find(params[:id)
end

因此,如果加入失败,您将无权访问。

另一种方法可以使用scopes来完成。您确保所有查询的任务都与候选人和 current_account 连接。

我还可以做一个before_filter对候选表进行独立查询,以检查该帐户是否具有访问权限。这将添加一个额外的查询,因此并不理想。

我在这里胡扯...但很想知道其他人是怎么做的?

4

1 回答 1

0

用铁轨做...

current_account = Account.find 参数[:id]

@tasks = current_account.candidates.collect(&:tasks)

这将完成你的工作。

于 2013-01-21T06:35:35.387 回答