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?
From stackoverflow
luca
-
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.
From Greg Campbell -
As long as you've specified the relationship in the model, Job will automagically have a build method:
@job.docs.build(params[:doc])
From Pesto -
# initialize the object @job.docs.build(params[:doc]) # create the object @job.docs.create(params[:doc])
Rafe : To be clear, the difference is that "create" goes ahead and inserts the new object into the database.From Simone Carletti
0 comments:
Post a Comment