Rails 3: Create an instance with 3 foreign keys.

Posted by donald on Stack Overflow See other posts from Stack Overflow or by donald
Published on 2010-12-22T12:45:57Z Indexed on 2011/02/08 7:25 UTC
Read the original article Hit count: 173

Hello,

Having a reviews table:

# Table name: reviews
#
#  id          :integer         not null, primary key
#  wsp_id      :integer
#  service_id  :integer
#  user_id     :integer
#  description :text
#  rating      :integer
#  created_at  :datetime
#  updated_at  :datetime
#

  belongs_to :wsp
  belongs_to :service
  belongs_to :user

How can I create a review for a service and pass the wsp_id and user_id? Do I need to use nested routes?

I am able to do @user.reviews.new(params[:review]) but I'm not being able of passing the wsp_id and the service_id.

Here's my Reviews create controller.

 def create
    @review = current_user.reviews.new(params[:review]) 

    if @review.save
      #Saved
    else
      #Error, not saved
    end
  end

What am I doing wrong?

Thank you!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby-on-rails-3