views:

308

answers:

3

this is what I mean:

job has many docs. I want to create a doc, I can do:

@doc = Doc.new(params[:doc])

but I'd like to enforce the parent-child relationship, since I already know the job.. something like this:

@job.docs.new(params[:doc])

so that the job_id field gets ignored and only the @job object matters...

does it make any sense?

+3  A: 

You should be able to use the build method:

@job.docs.build(params[:doc])

See the has_many api documentation or the Rails Guide for associations for a list of methods available on the collection.

Greg Campbell
A: 

As long as you've specified the relationship in the model, Job will automagically have a build method:

@job.docs.build(params[:doc])
Pesto
A: 
# initialize the object
@job.docs.build(params[:doc])
# create the object
@job.docs.create(params[:doc])
Simone Carletti
To be clear, the difference is that "create" goes ahead and inserts the new object into the database.
Rafe