我有三个模特
belongs_to :hiring_procedure)has_many :jobs, has_many :hiring_procedure_stages)belongs_to :hiring_procedure)这意味着,在我显示职务的视图中,我可以访问不同的招聘阶段,如下所示,这些阶段运行良好。
job.hiring_procedure.hiring_procedure_stages我的问题是,上面的代码将返回所有阶段,如果我只希望模型hiring_procedure_stage的hiring_procedure_stage列是f的阶段
我实际上是在下拉菜单中显示不同的阶段,供用户选择,我不希望用户看到已删除的阶段(删除的意思是删除值为模型hiring_procedure_stage的hiring_procedure_stage,它是布尔值)。
<%= select_tag
'application_stage',
options_for_select(
job.hiring_procedure.hiring_procedure_stages.map{ |p|[p.name, p.id] },
hiring_procedure_stage_id
),%>是否有一种方法可以在视图中实现这一目标,或者更好的方法是将此逻辑放入另一个变量中并访问该变量?
发布于 2017-06-22 11:06:03
在hiring_procedure_stage.rb模型中创建作用域
scope :active, -> { where(deleted: false) }现在,您可以将上述范围用作:
job.hiring_procedure.hiring_procedure_stages.active这只会返回hiring_procedure_stages,其中deleted is false
发布于 2017-06-22 11:37:35
另一种方法是定义与该范围的关系,因此将始终考虑未删除的阶段。
为此,请向关联添加作用域:
class HiringProcedure < ApplicationRecord
has_many :hiring_procedure_stages, -> { where(deleted: false) }
end每次你用
hiring_procedure.hiring_procedure_stages您将得到所有未被删除的阶段。
当然,只有当您永远不会尝试删除的阶段时,才会推荐此解决方案。
https://stackoverflow.com/questions/44697504
复制相似问题