views:

278

answers:

2

Hey all,

Currently, in my request model I have:

belongs_to :requestor, :class_name => 'User'

So the requestor is the current_user.

Problem is when the current_user clicks the create button to submit a form for a request, all of the attributes get updated to the database that are in the form.
But since requestor_id is not a value to fill out in the form, that brings back a value of null in the database when the new request record is created.
What I want is an integer (which equates to the primary key of the Users table) updated in the requestor_id column in the request table when the user clicks the create button.
So I thought that maybe adding a requestor_id as a symbol in the params for the create action would solve that:

def create_common
  @a = Request.new
    b = @a.requestor_id
  @resource = yield params[:contact + "#{b}".to_sym]
  self.resource = @resource

But instead it returns the following error:

interning empty string

Thanks for any suggestions.

+2  A: 

Can you not just pass the Request the current_user when you create it?

@req = Request.new(:requestor => current_user)

I am not quite sure what the yield params statement is meant to be doing,

Toby Hede
A: 

If I'm understanding your question correctly, you want to assign the current_user's id as the requestor id to the Request model?

# assign values passed in via form
@request = Request.new(params[:request])
# assign current_user to request
@request.requestor = current_user
# save the request
@request.save!

Hope this helps!

Paul Davis
This ended up working:@resource.requestor = current_user The only thing I'm not sure is how by doing what was done above does that assign the user primary key to the requestor_id. I would think that we would have to specify requestor_id, but that obviously isn't the case. Any insight into how this works? thanks.
JohnMerlino
In Rails, when you assign an object to relationship (eg: current_user which is a User object to requestor), it automatically extracts the ID of the object (in this case User's ID) and assigns it to the foreign key based on the relationship (in this case requestor_id). You can find detailed source code of this functionality within the ActiveRecord gem.
Paul Davis
thanks for the explanation.
JohnMerlino