我试图添加一个方法到我的seed.rb,所以我不必写一堆详细的代码.但是,根据create_deliverable方法的位置,在运行db:setup时,我会收到两个错误消息之一.
rake aborted!
private method ‘create_deliverable’ called for #
rake aborted!
undefined method `create_deliverable’ for #
是不是可以在seed.rb中使用方法?我以某种方式不正确地调用这个方法(我试过和没有自我调用).
def create_deliverable(complexity,project_phase_id,deliverable_type_id) Deliverable.create(:name => (0...8).map{65.+(rand(25)).chr}.join,:size => 2 + rand(6) + rand(6),:rate => 2 + rand(6) + rand(6),:deliverable_type_id => deliverable_type_id,:project_phase_id => project_phase_id,:complexity => complexity) end
呼叫码
@wf_project.project_phases.each do |phase| DeliverableType.find_by_lifecycle_phase(phase.lifecycle_phase_id).each do |type| self.create_deliverable("Low",type.id,phase.id) self.create_deliverable("Medium",phase.id) self.create_deliverable("High",phase.id) end end
解决方法
看起来像我在你的脚本中的“私有”访问修饰符之后放置你的create_deliverable方法.公开之后
public def create_deliverable(complexity,deliverable_type_id) Deliverable.create(:name => (0...8).map{65.+(rand(25)).chr}.join,:complexity => complexity) end private # to keep the rest of methods private