是否ActiveRecord的节省belongs_to的关联储蓄主要目的是什么时候? [英] Does ActiveRecord save a belongs_to association when saving main object?
本文介绍了是否ActiveRecord的节省belongs_to的关联储蓄主要目的是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有两个型号:
class Post < ActiveRecord::Base
belongs_to :user
end
和
class User < ActiveRecord::Base
has_many :posts
end
如果我做的:
post = Post.new
user = User.new
post.user = user
post.save
请问用户得到保存,以及在后
的 USER_ID
字段的主键指定是否正确?
Does the user get saved as well and the primary key properly assigned in post
's user_id
field?
推荐答案
ActiveRecord的 belongs_to的
协会有随父母模型被自动保存的功能,但该功能是关闭默认。要启用它:
ActiveRecord belongs_to
associations have the ability to be autosaved along with the parent model, but the functionality is off by default. To enable it:
class Post < ActiveRecord::Base
belongs_to :user, :autosave => true
end
这篇关于是否ActiveRecord的节省belongs_to的关联储蓄主要目的是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文