英文:
I deleted an object and attempt to save it again but Rails does not allow it
问题
我是新手使用Ruby on Rails,在尝试构建一个简单的Web应用时,我遇到了这个错误:
- 我有两个模型:User(用户)和Post(帖子)
- 用户可以拥有多个帖子
我创建了一个用户(user1
)和一个与该用户关联的帖子(post1
):
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
稍后,我使用 post1.destroy
删除了帖子。当我尝试使用 post1.save
再次保存它时,它返回了 false
,尽管 post1.valid?
返回了 true
,而 post1.errors
返回了一个空数组。我尝试了 post1.save!
,但它只返回了 无法保存记录(ActiveRecord::RecordNotSaved)
。我也没有任何回调方法。
有人能告诉我为什么会发生这种情况吗?非常感谢。
英文:
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?
returnedtrue
andpost1.errors
returned 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.
答案1
得分: 0
post1
对象具有 id 的值。
你使用 .save
方法,所以生成的查询将是 UPDATE ... where id = 1
而不是 CREATE ...
解决方案:
post1.dup.save
附言:你应该更多地了解 persistence。
你可以尝试通过使用 .new
创建对象,而不是使用已删除的对象,然后 save
,以查看查询。
更新
如果你真的想重新创建/恢复旧对象 post1
restore_post1 = post1.dup
restore_post1.assign_attributes(id: post1.id)
restore_post1.save
英文:
The post1
object has the id's value.
You use the method .save
so the query will be generated is
UPDATE ... where id = 1
not CREATE ...
Solution:
post1.dup.save
P/S: You should read more about persistence
You can try to create an object by .new
instead of using deleted object and then save
to see the query.
UPDATE
in case, you really want to re-create / restore the old object post1
restore_post1 = post1.dup
restore_post1.assign_attributes(id: post1.id)
restore_post1.save
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论