我删除了一个对象并尝试再次保存,但Rails不允许。

huangapple go评论57阅读模式
英文:

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: #&lt;User:0x00007fb82d27d3b8
  id: 1,
  username: &quot;Thao&quot;,
  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&gt;

    post1: #&lt;Post:0x00007fb82cc2f038
 id: 1,
 content: &quot;hi this is my first post&quot;,
 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&gt;
class User &lt; ApplicationRecord
    validates :username, presence: true, uniqueness: true, length: { in: 2..20 }

    has_many :posts
end

    class Post &lt; 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 with post1.save, it returned false , though post1.valid? returned true and post1.errors returned an empty array. I tried post1.save! and it just returned Failed 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

huangapple
  • 本文由 发表于 2023年7月3日 12:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定