Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 526637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:47:52+00:00 2026-05-13T08:47:52+00:00

I have an object called Review which :belongs_to two objects: Users and Vendors. In

  • 0

I have an object called Review which :belongs_to two objects: Users and Vendors. In other words, there is always a vendor writing a review for a specific vendor.

When I create a review, I tried to use the .build method to presumably add the foreign_key at the time the new record is created for review.vendor_id and review.user_id.

When I put into the controller for review the build statement for user id without one for capturing the vendor id, it works like a charm. When I add the vendor_id the user_id stays ‘nil’.

Help? Reviews is a nested resource: vendors/3/reviews/new

Forms for review/new:

<% form_for [@vendor, @review] do |f| %>

In the review controller:

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



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

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

end

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:47:52+00:00Added an answer on May 13, 2026 at 8:47 am

    This is your problem.

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

    You are overwriting @review with a new Review generated by @vendor.reviews.build. Instead of creating a new Review, you should just set the second foreign key using the methods created by the belongs_to association. Like this:

    @review = @current_analyst.reviews.build params[:review]
    @review.vendor_id = params[:vendor_id]
    

    To speed this process up, you can make :vendor_id part of the form_for @review. Assuming a Review is a nested resource of Vendor

    Example:

    Controller Actions:

    def new 
      @vendor = Vendor.find(params[:vendor_id])
      @review = @vendor.reviews.build
    end
    
    def create
      @review = @current_analyst.reviews.build params[:review]
      if @review.save
        redirect_to @review
      else
        render :action => :new
      end
    end
    

    Form partial:

    <% form_for @review do |f| %>
      ...
        Standard form information
      ...
      <%= f.hidden_field :vendor_id%>
      <%= submit_tag %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object called users that I use to store user objects that
I have an object called Customer which will be used in the other tables
I have an object class called BOM. It has a method, getChildItem() which returns
I have a parent object called Page that has a List of objects called
I have object called Foo. Right now it implements IFoo which has a lot
I have an object called EmailMessage, which has a nullable System.DateTime field called Timestamp.
I have an object called Reservations . Inside Reservations , there is a another
I have a table called Review and i want to retrieve all review objects
I have an object called Result<T> that has a constructor which accepts an argument
I have an object called exampleObject with two string properties: moduleName , and pool

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.