1
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

1
  • Sorry that should be @project. that was just a typo entering the question... Commented Feb 16, 2011 at 8:15

2 Answers 2

2

Try setting :autosave => true on your association

class Project < ActiveRecord::Base
  has_many :permissions, :autosave => true
  …
Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

That would merely assign nil to the Project instance's user_id if current_user.id is nil.
Now I know that #new and #build are aliases. So my answer is not true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.