Topic: how do I use create a new model instance that has two :belong_to?

I have a model object called Review that I set up as:

belongs_to :Vendor
belongs_to :User

This is a basic "review" site, so that, as a User, I want to post the review for a specific Vendor.

I have an object model called Review that belongs_to :Vendor and :User.

I can always pull the current user using @current_user because it is output from Authlogic.

When I write a review, I need to be able to store in the foreign key both the vendor_id and the user id **WITHOUT** using hidden_fields.  I know I can use .build method for one value but not both at the same time. 

I have created a nested route so that vendor/2/reviews/new allows me to capture the vendor_id for a review.

Currently, I use the following in my Review controller:

def new

@vendor = Vendor.find(params[:vendor_id])
@review = @vendor.reviews.build params[:review]
#@review = Review.new
end

def create

@vendor = Vendor.find(params[:vendor_id])
@review = @vendor.reviews.build params[:review]

#@review = Review.new(params[:review])
if @review.save
  flash[:notice] = "Successfully created review."
  redirect_to review_path(@review)
else
  render :action => 'new'
end
end

I haven't found a way to store the foreign_key for @current_user for review.user_id.

Last edited by angelacode (2010-01-03 14:39:34)

Re: how do I use create a new model instance that has two :belong_to?

Can't you do like that ? Is that you want ?   :

review = Review.new (:vendor =>@vendor, :user => @current_user)