如果我有两个型号:
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
用户是否也得到了保存,并且在post的user_id字段中正确分配了主键?
ActiveRecord belongs_to关联可以与父模型一起
自动保存,但默认情况下该
功能处于
关闭状态.要启用它:
class Post < ActiveRecord::Base
belongs_to :user,:autosave => true
end
原文链接:https://www.f2er.com/ruby/269780.html