Ruby on Rails createmethod不起作用

时间:2016-08-03 23:43:59

标签: ruby-on-rails ruby-on-rails-4 activerecord

我有一个模型day和一个模型taskday有很多tasks。我为此使用了nested_form。用户输入time和两个变量,这些变量计算为index。第一项任务index最高,starttime =早上8点 现在,我想按tasks订购index,并将每个任务time添加到上一个任务的starttime

我试图解决这个问题:

def create
  @day = current_user.days.build(day_params)
  @day.save

  @day.tasks.each do |task|
    task.index = task.ur**2 + task.imp

  end

  if current_user.worktype = 1
    @tasks = @day.tasks.order(index: :desc)
    x = 0
    @tasks.each do |task|
      if x = 0
        task.starttime = Time.new.beginning_of_day + 8*60*60
        x = task.id
      else
        task.starttime = @day.task.find(x).starttime + @day.task.find(x).time*60
        x = task.id
      end
    end
  elsif current_user.worktype = 2
   ...
  end
  @day.save

  respond_to do |format|
    if @day.save
      format.html { redirect_to @day, notice: 'Day was successfully created.' }
      format.json { render :show, status: :created, location: @day }
    else
      format.html { render :new }
      format.json { render json: @day.errors, status: :unprocessable_entity }
    end
  end
end

但是当我想在starttime

打印出来时,view仍然是零
- @tasks.each do |task|
  ...
  = task.starttime.strftime("%H:%M")

我也在rails console中查了一下。

POST的控制台日志:

Started POST "/days" for ::1 at 2016-08-04 02:19:03 +0200
Processing by DaysController#create as HTML
Parameters: {"utf8"=>"V",  "authenticity_token"=>"YaLq2XBUMltzCpZxvKBp5NQGUgiw/Ockto1r0zy/dZHU3HVlp4lpcsH/b3Q9WYas97ENlwRiPzCUdOiBC06GbA==", "day"=>{"tasks_attributes"=>{"1470269934695"=> {"description"=>"1", "ur"=>"1", "imp"=>"1", "time"=>"1"
, "_destroy"=>"false"}, "1470269939280"=>{"description"=>"2", "ur"=>"3",  "imp"=>"3", "time"=>"2", "_destroy"=>"false"}}}, "commit"=>"Create Day"}
User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?   ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
 (0.0ms)  begin transaction
SQL (3.5ms)  INSERT INTO "days" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?)  [["user_id", 1], ["created_at", "2016-08-04 00:19:03.986762"],  ["updated_at", "2016-08-04 00:19:03.986762"]]
SQL (0.0ms)  INSERT INTO "tasks" ("description", "ur", "imp", "time", "day_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["description", "1"], ["ur", 1], ["imp", 1], ["time", 1], ["day_id", 11], ["created_at", "2016-08-04 00:19:03.992775"], ["updated_at", "2016-08-04 00:19:03.992775"]]
SQL (0.0ms)  INSERT INTO "tasks" ("description", "ur", "imp", "time", "day_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)  [["description", "2"], ["ur", 3], ["imp", 3], ["time", 2], ["day_id", 11], ["created_at", "2016-08-04 00:19:03.994776"], ["updated_at", "2016-08-04 00:19:03.994776"]]
 (4.0ms)  commit transaction
Task Load (0.5ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."day_id" = ?  [["day_id", 11]]
Task Load (0.5ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."day_id" = ?  ORDER BY "tasks"."index" DESC  [["day_id", 11]]
(0.0ms)  begin transaction
SQL (1.0ms)  UPDATE "tasks" SET "index" = ?, "updated_at" = ? WHERE "tasks"."id" = ?  [["index", 2], ["updated_at", "2016-08-04 00:19:04.006796"], ["id", 24]]
SQL (1.0ms)  UPDATE "tasks" SET "index" = ?, "updated_at" = ? WHERE "tasks"."id" = ?  [["index", 12], ["updated_at", "2016-08-04 00:19:04.009792"], ["id", 25]]
(3.6ms)  commit transaction
(0.0ms)  begin transaction
(0.0ms)  commit transaction
Redirected to http://localhost:3000/days/11
Completed 302 Found in 39ms (ActiveRecord: 14.6ms)

修改

在@ evanbike的回答基础上,每次task.save设置时我都会添加starttime。但是没有任何改变,所以我尝试删除了If语句,现在保存了starttime,但每个task都有相同的时间。

@tasks = @day.tasks.order(index: :desc)
x = 0
@tasks.each do |task|
  if x = 0
    task.starttime = Time.new.beginning_of_day + 8*60*60
    task.save
    x = task.id
  else
    task.starttime = @day.task.find(x).starttime + @day.task.find(x).time*60
    task.save
    x = task.id
  end
end
@day.save

我希望有人可以帮我解决这个问题 提前谢谢。

1 个答案:

答案 0 :(得分:0)

执行此操作时,您要在index关联缓存中的task实例上设置@day

@day.tasks.each do |task|
  task.index = task.ur**2 + task.imp
end

然后,当你这样做时:

@tasks = @day.tasks.order(index: :desc)

...它进行数据库调用(因为你正在调用order)并返回没有设置index的新实例。如果您调用sort或某些数组方法,它将使用存储的实例

我认为在设置每个值后,最简单的将是save任务实例。在关联上调用sort 可能工作,但它似乎很脆弱。