I'm new to Ruby on Rails and when I'm trying to build a simple web app, I stumbled this error:
- I have 2 models: User and Post
- User can have many posts
I created a user(
user1) and a post(post1) associated with that user:
user1: #<User:0x00007fb82d27d3b8
id: 1,
username: "Thao",
password: nil,
email: nil,
age: nil,
created_at: Mon, 03 Jul 2023 01:13:07.787945000 UTC +00:00,
updated_at: Mon, 03 Jul 2023 01:13:07.787945000 UTC +00:00,
phone_number: nil>
post1: #<Post:0x00007fb82cc2f038
id: 1,
content: "hi this is my first post",
user_id: 1,
created_at: Mon, 03 Jul 2023 02:18:35.058605000 UTC +00:00,
updated_at: Mon, 03 Jul 2023 02:18:35.058605000 UTC +00:00>
class User < ApplicationRecord
validates :username, presence: true, uniqueness: true, length: { in: 2..20 }
has_many :posts
end
class Post < ApplicationRecord
validates :content, presence: true, length: { minimum: 10 }
validates :user_id, presence: true
belongs_to :user
end
Later, I deleted the post with
post1.destroy. When I tried to save it again withpost1.save, it returnedfalse, thoughpost1.valid?returnedtrueandpost1.errorsreturned an empty array. I triedpost1.save!and it just returnedFailed to save the record (ActiveRecord::RecordNotSaved). I also did not have any callback methods.Can anyone tell me why this happened ? Thank you very much.
The
post1object has the id's value.You use the method
.saveso the query will be generated isUPDATE ... where id = 1notCREATE ...Solution:
P/S: You should read more about persistence You can try to create an object by
.newinstead of using deleted object and thensaveto see the query.UPDATE
in case, you really want to re-create / restore the old object
post1