The order of records (id) in the table will be changed after bulk update in the Rails app's Activerecord.

Asked 1 years ago, Updated 1 years ago, 88 views

We have made a new registration as follows.
After that, I pressed the Edit button to change the product name from Product B to Hogehoge.

"2 Stainless Steel BF-078 10K 350A 3P 2,000 3,000 4,000 9,000"
"2 Stainless steel hogehoge F-078 10K 350A 3P 2,000 3,000 4,000 9,000"
After that, when I pressed the "Register" button, the order of the records was changed and displayed.
I also checked the contents of the costs table and found that the order of the records was changed and registered (updated).
Is there a possible cause?
If there is a reason that comes to mind, please advise me.
Sorry for the URL.
Thank you for your cooperation.

https://github.com/hiro-hvk/sample_order_app

What you entered when registering for a new one
Code Material/Part No Product Name Type Pressure Size Quantity Unit Supplier A Supplier B Supplier C Sales Price
1 Stainless Steel Products AF-065 10K 300A 5P 1,000 2,000 6,000
2 Stainless steel products BF-078 10K 350A 3P 2,000 3,000 4,000 9,000
3 Stainless Steel Products CF-084 10K 400A 6P 3,000,000 8,000

What to enter when updating
Code Material/Part No Product Name Type Pressure Size Quantity Unit Supplier A Supplier B Supplier C Sales Price
1 Stainless Steel Products AF-065 10K 300A 5P 1,000 2,000 6,000
2 Stainless steel hogehoge F-078 10K 350A 3P 2,000 3,000 4,000 9,000
3 Stainless Steel Products CF-084 10K 400A 6P 3,000,000 8,000

View after update
Code Material/Part No Product Name Type Pressure Size Quantity Unit Supplier A Supplier B Supplier C Sales Price
1 Stainless Steel Products AF-065 10K 300A 5P 1,000 2,000 6,000
3 Stainless steel products CF-084 10K 400A 6P 3,000,000 8,000
2 Stainless steel hogehoge F-078 10K 350A 3P 2,000 3,000 4,000 9,000

追Additional

costs_controller.rb

def index
  @costs=Cost.all.order(:id)
end

subjects_controller.rb

private

default_subject
  @subject=Subject.find (params[:id])
  @[email protected](:id)
end

In views/subjects/show.html.erb, we were able to put them in the 」id 順 order of the costs table.

Code Material/Part No Product Name Type Pressure Size Quantity Unit Supplier A Supplier B Supplier C Sales Price
1 Stainless Steel Products AF-065 10K 300A 5P 1,000 2,000 6,000
2 Stainless steel hogehoge F-078 10K 350A 3P 2,000 3,000 4,000 9,000
3 Stainless Steel Products CF-084 10K 400A 6P 3,000,000 8,000

However, when I pressed the Edit button after that, the _form.html.erb in views/subjects/edit.html.erb kept the order of the records changing.The following conditions were found:

Code Material/Part No Product Name Type Pressure Size Quantity Unit Supplier A Supplier B Supplier C Sales Price
1 Stainless Steel Products AF-065 10K 300A 5P 1,000 2,000 6,000
3 Stainless steel products CF-084 10K 400A 6P 3,000,000 8,000
2 Stainless steel hogehoge F-078 10K 350A 3P 2,000 3,000 4,000 9,000

Is it possible to arrange this page in the order of "id" in the costs table?
Currently, the order of records in the costs table has been changed since the update method was executed.

def update
    response_to do | format |
      [email protected](subject_params)
        format.html {redirect_to@subject,notice: 'Subject was successfully updated.'}
        format.json {render:show,status::ok,location:@subject}
      else
        format.html {render:edit}
        format.json {render json:@subject.errors, status::unprocessable_entity}
      end
    end
 end

Thank you for your cooperation.

ruby-on-rails ruby rails-activerecord postgresql

2022-09-30 19:07

2 Answers

I think it's a costs#index, but the controller says:

def index
  @costs=Cost.all
end

The order is not guaranteed unless explicitly stated in the SQL database.If the order is a problem (which may change with each call) then sort it using .order as needed.


2022-09-30 19:07

As yasu wrote, be sure to add order to the order you want.

In particular, you seem to be using PostgreSQL, but the Coates writes data in the postscript format, so when you update it, it says, "Add a record to the end of the table with new content so you don't use the original record."


2022-09-30 19:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.