在Model-1脚手架中创建动作,
def create
@auclub = Auclub.new(auclub_params)
user = User.find(current_user.id)
respond_to do |format|
if @auclub.save && user.update(auclub_id: @auclub.id)
format.html { redirect_to @auclub, notice: 'Auclub was successfully created.' }
format.json { render :show, status: :created, location: @auclub }
else
//some codes
end
end
end
并且我希望与用户一起创建俱乐部
此代码有效,但如果我这样使用
user = User.find(current_user.id)
user.new(auclub_id : @auclub.id)
user.save
它不起作用! update和new.save之间有什么区别吗?
答案 0 :(得分:2)
尝试:
@auclub = Auclub.new(auclub_params)
@user = User.find(current_user.id)
@auclub.save
@user.auclub_id = @auclub.id
@user.save
了解更多APi dock
答案 1 :(得分:0)
你正在以错误的方式做事。
如果没有型号和其他代码,我可以建议这样做:
issueFunction in previousSprint("<board name>")
我认为在您的情况下,user = User.find(current_user.id)
user.auclub_id = @auclub.id
user.save
等ActiveRecord实例没有new
方法可用。
user
来创建一个新实例,如:
new
我不认为使用user = User.new
方法和手动重新分配属性并使用update
方法保存对象之间存在很大差异,但您必须密切关注性能,DRY原则和在模型上定义的回调。