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 8809027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:46:10+00:00 2026-06-14T02:46:10+00:00

I am trying to create a nested attribute form to create a model which

  • 0

I am trying to create a nested attribute form to create a model which is primarily an association “connector” between two other models. In my case, the models represent books, awards, and the “connector” model book_awards. When I am editing a book, I want to be able to quickly select which awards it has won.

I’ve been using
http://railscasts.com/episodes/196-nested-model-form-part-1
to help me get started, but I’m afraid I’m pretty much stuck.

Another SO question which seems similar is
accepts_nested_attributes_for with find_or_create? Unfortunately, it’s also not quite what I’m doing and I haven’t been able to adapt it.

My models look like this. Each model has additional attributes and validations etc, but I’ve removed them for clarity.

class Book < ActiveRecord::Base
  has_many :book_awards
  accepts_nested_attributes_for :book_awards, :allow_destroy => true
end

class Award < ActiveRecord::Base
  has_many :book_awards
end

class BookAward < ActiveRecord::Base
  belongs_to :book, :award
end

In my book controller methods for edit and new, and the failure cases for create and update I have a line @awards = Award.all.

In my view, I would like to see a list of all awards with check boxes next to them. When I submit, I would like to either update, create, or destroy a book_award model. If the check box is selected, I would like to update an existing model or create a new one if it doesn’t exist. If the check box isn’t selected, then I would like to destroy an existing model or do nothing if the award never existed. I have a partial for book_awards. I’m not sure if the check box selector should be in this partial or not.

I think my check box will be my hook to :_destroy but with its polarity reversed. I think something like this will basically do it:

= f.check_box :_destroy, {}, 0, 1

Currently, I have this in my partial but I’m not sure where it really belongs.

Next comes my view which currently doesn’t work, but maybe it will help demonstrate what I’m trying to do. I loop through the awards and use a fields_for to set nested attributes for anything that already exists. It’s horribly ugly, but I think it somewhat works. However, I don’t really know how to get started with the else case.

= f.label :awards
- @awards.each do |a|
  - if f.object.awards && f.object.awards.include?(a)
    = f.fields_for :book_awards, f.object.book_award.select{|bas| bas.award == a } do |ba|
      = render 'book_awards', :f => ba, :a => a
  - else
    = fields_for :book_awards do |ba|
      = render 'book_awards', :f => ba, :a => a

I would prefer the awards to be listed in the same order each time (my @awards assignment in the controller will probably specify the order) as opposed to listing the existing awards first or last.

  • 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-06-14T02:46:11+00:00Added an answer on June 14, 2026 at 2:46 am

    I hate to answer my own question, but I finally figured out something which works. The first thing I needed to do was to update the “new” case based on the crazy object which was included in the railscast. Next, I needed to manually set the :child_index. Finally, I needed to manually set the :_destroy check box appropriately.

    .field
      = f.label :awards
      - @awards.each_with_index do |a,i|
        - if exists = (f.object.awards && f.object.awards.include?(a))
          - new_ba = f.object.book_awards.select{|s| s.award == a}
        - else
          - new_ba = f.object.class.reflect_on_association(:book_awards).klass.new
        = f.fields_for :book_awards, new_ba, :child_index => i do |ba|
          = render 'book_awards', :f => ba, :a => a, :existing => exists
    

    My partial looks like this:

    .field
      = f.check_box :_destroy, {:checked => existing}, 0, 1
      = f.label a.name
      = f.hidden_field :award_id, :value => a.id
      = f.label :year
      = f.number_field :year
    

    It’s not horribly pretty, but it seems to do exactly what I wanted.

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

Sidebar

Related Questions

I'm trying to create a form handling 4 models at once with 2 nested
I'm trying to add has_many_through relationship through a form nested in a parent model.
How do I create a nested form for 2 scaffolded models? I have a
I am trying to set up a nested model form similar to the one
I'm trying to create a nested form but I'm missing something as keep getting
I am trying to create a nested form using formtastic. I've included my code
I'm trying to add a fields_for attribute to a nested rails form. Any time
I'm trying to create a simple User & Address Model. I've created a nested
I am trying to create nested routes for a project that has many boards.
What I am trying to do: I am trying to create nested groups but

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.