Rails 3 - 如何从 mysql 表中查找最后插入的 ID

发布于 2024-11-13 22:36:45 字数 307 浏览 6 评论 0原文

在我的控制器中,我有以下命令序列:

SAVE DATA INTO FIRST TABLE

_get ID of inserted item into table from first step_

SAVE DATA INTO SECOND TABLE WITH ID FROM FIRST COMMAND

if FIRST.save && SECOND.save
 do something

我想知道如何获取立即插入数据库的项目ID...我尝试谷歌搜索,但找不到此信息...

提前致谢给你的提示

in my controller I have following sequence of commands:

SAVE DATA INTO FIRST TABLE

_get ID of inserted item into table from first step_

SAVE DATA INTO SECOND TABLE WITH ID FROM FIRST COMMAND

if FIRST.save && SECOND.save
 do something

And I am wondering, how to get id of item, which is immediately inserted into database... I tried to googling, but I can't find this information...

Thanks in advance for your hints

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱她像谁 2024-11-20 22:36:45
# SAVE DATA INTO FIRST TABLE
first_instance = FirstModel.new( :foo => :bar )
first_save = first_instance.save

# _get ID of inserted item into table from first step_
first_instance_id = first_instance.id

# SAVE DATA INTO SECOND TABLE WITH ID FROM FIRST COMMAND
second_save = SecondModel.new( :first_model_id => first_instance_id ).save

if first_save && second_save
  # do something
end
# SAVE DATA INTO FIRST TABLE
first_instance = FirstModel.new( :foo => :bar )
first_save = first_instance.save

# _get ID of inserted item into table from first step_
first_instance_id = first_instance.id

# SAVE DATA INTO SECOND TABLE WITH ID FROM FIRST COMMAND
second_save = SecondModel.new( :first_model_id => first_instance_id ).save

if first_save && second_save
  # do something
end
╭⌒浅淡时光〆 2024-11-20 22:36:45

保存模型后,您可以访问它的 id 变量:

@user = User.new
puts @user.id
# => nil
@user.save
puts @user.id
# => 1

After saving a model, you can access it's id variable:

@user = User.new
puts @user.id
# => nil
@user.save
puts @user.id
# => 1
つ低調成傷 2024-11-20 22:36:45

您可以通过模型中的 updated_at 字段搜索数据库吗?

要获取最新记录:

@model1 = Model1.order("updated_at DESC").limit(1)

或者更好的是,在首先保存 Model1 时:

@model1 = model1.save

分配:

@model2.model1_id = @model1.id

注意:如果您确实想要保存特定记录的 ID,则查找最后一条记录并不可行最好的方法。

这是因为在插入 Model1 之后和调用 Model2 之前,其他用户可能会插入另一条记录。

如果您希望两者一起保存或根本不保存,您可以查看事务: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

如果您对 Model1 在担心 Model2 之前自行保存感到满意,那么只需像我上面那样分配变量即可。

Could you just search your database by the updated_at field in your model?

To get the most recent record:

@model1 = Model1.order("updated_at DESC").limit(1)

or better yet, upon saving Model1 in the first place:

@model1 = model1.save

To assign:

@model2.model1_id = @model1.id

Note: if you actually want to save the ID of a specific record, finding the last isn't the best way to go.

This is because another record could be inserted by a different user, right after you inserted Model1 and right before you call Model2.

If you want the two to save together or not at all, you can look into transactions: http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html

If you're happy with Model1 saving on its own before worrying about Model2, then simply assign the variables as I did above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文