我有一个模型day
和一个模型task
。 day
有很多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
我希望有人可以帮我解决这个问题 提前谢谢。
答案 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
可能工作,但它似乎很脆弱。