RoR方法,用于搜索表中的数据,然后根据该数据更新另一个表上的另一列

时间:2014-09-03 16:54:45

标签: ruby-on-rails

我有一个程序,我正在改变我的工作。我知道我必须做什么,但我在使代码工作时遇到问题。我来自Java和C背景。

我有两个表,一个名为customprojecschedule_lines的表有一个project_id,workorder_base_id和其他各种列列。

另一个名为customschedule的表有一个id,workorder列和其他各种列。

我有一个名为工单的方法和变量。

我正在尝试获取一个SQL语句,就像这样:

class Customschedule < ActiveRecord::Base
set_table_name "customschedules" 
after_create :build_customprojectschedule_lines
has_many :customprojectschedule_lines, :dependent => :destroy

has_one :projectschedule_cost
delegate :est_cost, :act_cost, :to => :projectschedule_cost, :allow_nil => true

attr_accessor :workorder_base, :lots

def workorder
customschedule.where(:id => customprojectschedule_lines.product_id)
end

 def workorder=(wo)
@workorder_base = wo
customprojectschedule_lines.each do |pl|
  pl.update_attributes({:workorder_base_id => wo})
 end
end

def build_customprojectschedule_lines
lines = @lots.split(',').inject([]) do |lines, lot_id|
  line = customprojectschedule_lines.find_or_initialize_by_workorder_lot_id(lot_id)
  if line.new_record?
    p workorder_base
    line.workorder_base_id = @workorder_base
    line.line_no = lot_id
    line.workorder_split_id = 0
  end
  lines << line
end
customprojectschedule_lines.replace(lines)
end

基本上我想要的是,每当用户在表单编号上输入工作订单进入数据库时​​,获取该记录的存储值,然后检索ID(来自该记录)并将相同的id放入我的另一个表中

但是,我一直收到这个错误:

undefined local variable or method `customschedule' for #          
<Customschedule:0x00000005542040>

在我尝试的事情之前,我不断得到一个奇怪的选择声明,表示Customschedule ID为空。

我们在这里使用oracle。

这是我第一次在这里发帖,所以如果我还需要其他任何内容,请告诉我。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为你需要的只是大写这一行的第一个字母

customschedule.where(:id => customprojectschedule_lines.product_id)

将其更改为

Customschedule.where(:id => customprojectschedule_lines.product_id)