# Fields\_for/pseudoattribute approach failing on create parent (b/c parent not saved?)

**URL:** <https://www.ruby-forum.com/t/fields-for-pseudoattribute-approach-failing-on-create-parent-b-c-parent-not-saved/142656>\
**Category:** Rails\
**Created:** [July 10, 2008, 2:28am UTC](https://www.ruby-forum.com/t/fields-for-pseudoattribute-approach-failing-on-create-parent-b-c-parent-not-saved/142656 "2008-07-10T02:28:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [July 10, 2008, 2:28am UTC](https://www.ruby-forum.com/t/fields-for-pseudoattribute-approach-failing-on-create-parent-b-c-parent-not-saved/142656/1 "2008-07-10T02:28:51Z")

</div>

Hey All,

I’m trying to use the railscasts ep 75 approach to including multiple  
models on a single form, by using fields\_for to load up a  
pseudoattribute. My ‘parent’ model is a Project, and the ‘child’ is a  
ProjectPerson. It works fine when I’m editing an existing project, but  
when I’m creating a new project, I get a validation error for each  
ProjectPerson: “Project can’t be blank”. I’m guessing that means that  
the project’s id is not getting copied over to the ProjectPerson’s  
project\_id, but I’m not sure how to make that happen. I expected it to  
happen automatically (shouldn’t it?), but even if I try to do it myself  
explicitly (see below) it doesn’t seem to work. I’m using rails v2.0.2.

Here is an example of the parameters that my projects/new form hands to  
the projects/create action:

Parameters:  
{“commit”=\>“Create”  
, “project”=\>  
{“name”=\>“Roys Cool Project”  
, “abbreviation”=\>“RCP”  
, “funding\_mechanism”=\>""  
, “new\_research\_area\_attributes”=\>[{“research\_area\_id”=\>""}]  
, “grant\_number”=\>""  
, “id”=\>""  
, “new\_project\_person\_attributes”=\>[{“role”=\>“Programmer”,  
“person\_id”=\>“9”}]  
, “description”=\>""  
, “status\_id”=\>“2”  
, “funder\_id”=\>""  
, “start\_date(1i)”=\>""  
, “start\_date(2i)”=\>""  
, “start\_date(3i)”=\>""  
, “end\_date(1i)”=\>""  
, “end\_date(2i)”=\>""  
, “end\_date(3i)”=\>""}  
, “#ProjectPerson:0x4ad1494”=\>{“role”=\>“Programmer”}  
, “authenticity\_token”=\>“08f382d5ec07eecec7cd229ce67664094516ed26”  
, “action”=\>“create”  
, “#ProjectPerson:0x4ad6a70”=\>{“role”=\>“Programmer”}  
, “controller”=\>“projects”  
, “name\_search”=\>“r”}

Projects/create is scaffold-written–it just does a

@project = Project.new(params[:project])  
respond\_to do |format|  
if @project.save  
flash[:notice] = ‘Project was successfully created.’  
\<\<etc.\>\>

The relevant bits of the project model are:

class Project \< ActiveRecord::Base  
has\_many :project\_person, :dependent =\> :delete\_all

```
after_update :save_personnell

def new_project_person_attributes=(personnell)
  personnell.each do |person|
    project_person.build(person)
  end
end

def save_personnell
  # raise("boobies!")
  project_person.each do |p|
    p.project_id = id # <-- no effect!
    p.save(false)
  end
end

```

end # class Project

Can any of you kind folks advise on how I can fix (or further debug)  
this problem?

Thanks!

-Roy

Roy P.  
Research Analyst/Programmer  
Group Health Center For Health Studies (Cancer Research Network)  
(206) 287-2078  
Google Talk: rpardee

---

<div class="post-metadata">

**Author:** ![rpardee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e47c2d/32.png) [@rpardee](https://www.ruby-forum.com/u/rpardee)\
**Post date:** [July 11, 2008, 2:58am UTC](https://www.ruby-forum.com/t/fields-for-pseudoattribute-approach-failing-on-create-parent-b-c-parent-not-saved/142656/2 "2008-07-11T02:58:40Z")

</div>

Found my problem. I was being anal-retentive, and had put this in my  
child model (ProjectPerson)

validates\_presence\_of :project\_id, :person\_id

If I comment that out, I can save the parent record no problem. So I  
guess AR is calling .valid? on the child before trying to save it to the  
db?

I guess I’ll have to ensure I don’t get nulls in those fields via db  
constraints.

Frustrating to have lost so much time to that…

Cheers,

-Roy
