I got a model day
and a model task
. day
has many tasks
. I'm using a nested_form for this. The user enters a time
and two variables, which a caculated to an index
. The first task, with the highest index
has a starttime
= 8am.
Now I want to order the tasks
by the index
and add every task's time
to the previous task's starttime
.
My attempt to solve this:
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
But somehow starttime
remains nil, when I want to print it out in the view
- @tasks.each do |task|
...
= task.starttime.strftime("%H:%M")
I checked it in rails console
too.
consolelog for 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)
Building on @evanbike's answer I added a task.save
everytime starttime
is set. But nothing changed, so I tried and removed the If statement and now starttime
is saved, but every task
has the same time.
@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
I hope someone can help me with this issue.
Thanks in advance.
When you do this, you are setting the index
on the instance of the task
in the @day
association cache:
@day.tasks.each do |task|
task.index = task.ur**2 + task.imp
end
Then, when you do this:
@tasks = @day.tasks.order(index: :desc)
...it makes a db call (since you're calling order
on it) and return new instances—that don't have index
set. If you called sort
or some array method, it would use the stored instances
I think the simplest would be to save
the instances of tasks after you set each of the values. Calling sort
on the association would probably work, but it seems brittle.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.