开发者

Rails - Building a nested model

开发者 https://www.devze.com 2023-02-11 06:20 出处:网络
Project (id) Permission (project_id, user_id) When trying to save a project, I get the following error: ActiveRecord::RecordNotSaved (You cannot call create unless the parent is saved):
Project (id)
Permission (project_id, user_id)

When trying to save a project, I get the following error: ActiveRecord::RecordNotSaved (You cannot call create unless the parent is saved):

Controller:

@project = current_user.projects.new(:name => params[:project][:name])
@project.permissions开发者_开发知识库.build(:user_id => current_user.id)

respond_to do |format|
  if @project.save
  ......

Suggestions? Thansk


Try setting :autosave => true on your association

class Project < ActiveRecord::Base
  has_many :permissions, :autosave => true
  …


current_user.projects.new doesn't set user_id for a project, try to use current_user.projects.build instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消