Newbie Question. I'm dumb with forms and foreign keys


#1

For some reason I am just not able to get this.

I am trying to setup an organizational hierarchy, so I have a
organization which works fine. From there I want to be able to define
new divisions, departments, areas, etc.

For creating a Division, my original thought was to pass the
organization’s id to the new organization unit form, and then do a
Division.new(:organization_id => params[:id]) but of course that fails
because it looks like doing a Object.new ignores any id or foreign keys
in the object.

I am assuming (and hoping) there is a simple solution or convention on
how to accomplish this and I’m just completely missing it.

Thanks,
Dave


#2

d = Division.new(params)
d.organization_id = params[:id]

One question I have for others, is there any reason or advantage to
use objects instead of purely the fkey? i.e.
d.organization = Organization.find(params[:id])

One simple reason is that you have fewer database issues. If you were
to change the fkey name, you simply need to update it in the model.
That’s unlikely though, and I was wondering if there are any practical
reasons why you might want to access it via association, which has the
overhead of pulling the record from the db.

Pat