繁体   English   中英

Ruby为什么没有ID的情况下保存我的对象?

[英]Ruby why is my object saving without an ID?

我觉得我缺少一些非常基本的东西,有人可以看一下并很快告诉我那是什么...

def create
    @user = User.find_by_id(create_params[:user_id])
    @plan = @user.plans.new(create_params)
    if @plan.save 
      puts @plan.inspect
    end
end

现在, puts语句返回以下内容:

#<Plan id: nil, zipcode: "02114", selected_plan: 5, user_id: 1>

所以基本上..所有属性都在那里,但是没有id ...

更多信息:

create_params = {zipcode:"02114", selected_plan:"5", user_id: 1}

如果知道对您有帮助,请save! 确实有效并且成功保存了带有ID的计划... and plan.save == true返回true

您的代码应具有以下模式。

    @user = User.find(create_params[:user_id]) # see changes
    @plan = @user.plans.new(create_params)
    if @plan.save 
      puts @plan.reload.inspect  #reload object to see if the object has been saved!
    else
      puts @plan.errors.messages.inspect  # if object is not saved there should be Object#errors.messages should have messages
    end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM