开发者

Rails - Building a nested model

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜