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

  • Home
  • SEARCH
  • 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 6146971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:02:12+00:00 2026-05-23T19:02:12+00:00

I am trying to set up a nested model form similar to the one

  • 0

I am trying to set up a nested model form similar to the one in Ryan Bates’ Railscast Ep #196.

My models are:

 class EmployeeReview < ActiveRecord::Base
  belongs_to :employee
  belongs_to :user
  has_many :review_ratings, :dependent => :destroy
  accepts_nested_attributes_for :review_ratings
  attr_accessible :employee_id


class ReviewRating < ActiveRecord::Base
 belongs_to :employee_review
 belongs_to :review_category

class ReviewCategory < ActiveRecord::Base
 has_many :review_ratings

I’ve already entered in Review Categories, and I have categories separated as a model in case I want to add categories in the future. To display all of the categories in my form, the ‘new’ action in my EmployeeReviews controller looks like this:

def new
 @title = "Employee Review"
 @review = EmployeeReview.new()
 @employee = Employee.find(params[:employee_id])
 @categories = ReviewCategory.all
  @categories.each do |category|
    @rating = @review.review_ratings.build({:review_category_id => category.id})
  end
end

Then I use the following code for my form:

<h2>Create a review for <%= @employee.first_name %> <%= @employee.last_name %></h2>

<%= form_for @review do |f| %>

<% f.fields_for :review_ratings do |builder| %>
 <b><%= builder.object.review_category.name %></b><br/>
 <%= builder.label :score, "Score" %><br/>
 <%= builder.text_field :score %><br/>
 <%= builder.label :comment, "Comment" %><br/>
 <%= builder.text_area :comment %><br/>
<% end %>

So the form works and displays the correct categories. However, when I submit, it creates the review, but isn’t saving the review_ratings associated with it. Here is the code in the ‘create’ action of my employee_review controller.

def create
 @employee = Employee.find(params[:employee_id])
 @review = EmployeeReview.new(params[:review])
 @review.user_id = current_user.id
 @review.employee_id = @employee.id
 if @review.save
  redirect_to @employee, :flash => {:success => "employee review was created"}
 else
  render 'new', :flash => {:error => "Employee review was not created"}
 end
end

I’m sure it’s something obvious, but being very new to rails I can’t quite figure it out, even after looking at other nested attribute posts. Any advice is much appreciated.

Update: Here is my schema (the sections for the models)

  create_table "review_categories", :force => true do |t|
   t.text     "description"
   t.datetime "created_at"
   t.datetime "updated_at"
   t.string   "name"
  end

  create_table "review_ratings", :force => true do |t|
   t.integer  "employee_review_id"
   t.integer  "score"
   t.text     "comment"
   t.datetime "created_at"
   t.datetime "updated_at"
   t.integer  "review_category_id"
  end

  create_table "employee_reviews", :force => true do |t|
   t.integer  "employee_id"
   t.datetime "created_at"
   t.datetime "updated_at"
   t.integer  "user_id"
  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-23T19:02:13+00:00Added an answer on May 23, 2026 at 7:02 pm

    Start with this and see if the fields_for actually shows up. By which I mean you actually have a review when you go to the new action.

    def new
     @title = "Employee Review"
     @review = EmployeeReview.new()
     @employee = Employee.find(params[:employee_id])
     @categories = ReviewCategory.all
     conditions = {:review_category_id => ReviewCategory.first.id}
     @rating = @review.review_ratings.build(conditions)
    end
    

    First we need to see if fields_for is actually working and then can do you custom stuff. Let me know what happens when you swap out this code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using nested set model for my menu tree, and I'm trying to get
I am trying to manage the retrieval of a node in a nested set
I have a set of nested Ant build files, and I need to control
I am trying to set a background image to be outside the actual containing
I'm trying to create a base class for my POCO objects in .NET 4,
If I have an object that is composed of nested complex types, e.g. a
I am trying to use LINQ to query a list of objects wherever appropriate.
I am trying to detect touches, but the touchesBegan method is not being called.
I'm trying to make a function template that will accept two (or more) of
I have a WebClient instance that receives data from a remote website. I referenced

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.